Skip to content

Instantly share code, notes, and snippets.

@literalpie
Last active January 28, 2021 21:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save literalpie/83f64673b9db00b6f7d7fa1f316dd3e6 to your computer and use it in GitHub Desktop.
Save literalpie/83f64673b9db00b6f7d7fa1f316dd3e6 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 }
let location = panGesture.location(in: self.view)
switch panGesture.state {
case .began:
// existing logic from previous approach. Keep this.
guard let hitNodeResult = view.hitTest(location, options: nil).first else { return }
panStartZ = CGFloat(view.projectPoint(lastPanLocation!).z)
// lastPanLocation is new
lastPanLocation = hitNodeResult.worldCoordinates
case .changed:
// This entire case has been replaced
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
}
}
@iMostfa
Copy link

iMostfa commented Jan 28, 2021

Thank you very much, it worked, btw, line 10 should replaced with line 8

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