Skip to content

Instantly share code, notes, and snippets.

@literalpie
Last active May 16, 2020 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save literalpie/3f6b06ddf9cffd540acb661fbfb2a0b2 to your computer and use it in GitHub Desktop.
Save literalpie/3f6b06ddf9cffd540acb661fbfb2a0b2 to your computer and use it in GitHub Desktop.
shows a simple pan gesture handler for use in part 2 of a literalpie medium post
@objc func handlePan(panGesture: UIPanGestureRecognizer) {
guard let view = view as? SCNView else { return }
let location = panGesture.location(in: self.view)
switch panGesture.state {
case .began:
guard let hitNodeResult = view.hitTest(location, options: nil).first else { return }
// panStartZ and draggingNode should be defined in the containing class
panStartZ = CGFloat(view.projectPoint(lastPanLocation!).z)
draggingNode = hitNodeResult.node
case .changed:
let location = panGesture.location(in: view)
let worldTouchPosition = view.unprojectPoint(SCNVector3(location.x, location.y, panStartZ!))
draggingNode?.worldPosition = worldTouchPosition
default:
break
}
}
@atulkhatri
Copy link

How to define: lastPanLocation variable?

@literalpie
Copy link
Author

literalpie commented May 16, 2020

You should be able to add it to the viewController, outside the function shown in this gist:

var lastPanLocation: SCNVector3?

@objc func handlePan(panGesture: UIPanGestureRecognizer) {
...
}

here is the appropriate code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment