Skip to content

Instantly share code, notes, and snippets.

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/05b0f4f5ff206fd2e00ab8ac7a0ddf0d to your computer and use it in GitHub Desktop.
Save literalpie/05b0f4f5ff206fd2e00ab8ac7a0ddf0d to your computer and use it in GitHub Desktop.
a better approach to handling the pan gesture in scenekit. For use in part 3 of medium post
@objc func handlePan(panGesture: UIPanGestureRecognizer) {
guard let view = view as? SCNView else { return }
switch panGesture.state {
case .began:
let location = panGesture.location(in: self.view)
guard let hitNodeResult = view.hitTest(location, options: nil).first else { return }
panStartZ = CGFloat(view.projectPoint(lastPanLocation!).z)
lastPanLocation = hitNodeResult.worldCoordinates
draggingNode = hitNodeResult.node
case .changed:
let location = panGesture.location(in: view)
let worldTouchPosition = view.unprojectPoint(SCNVector3(location.x, location.y, panStartZ!))
let movementVector = SCNVector3(
worldTouchPosition.x - lastPanLocation!.x,
worldTouchPosition.y - lastPanLocation!.y,
worldTouchPosition.z - lastPanLocation!.z)
geometryNode.localTranslate(by: movementVector)
self.lastPanLocation = worldTouchPosition
default:
break
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment