Created
May 19, 2019 16:33
-
-
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
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 } | |
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