Last active
January 28, 2021 21:35
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you very much, it worked, btw, line 10 should replaced with line 8