Skip to content

Instantly share code, notes, and snippets.

@shrawan2015
Created October 15, 2018 12:44
Show Gist options
  • Save shrawan2015/38d0b6043b230e46e024580cc9045123 to your computer and use it in GitHub Desktop.
Save shrawan2015/38d0b6043b230e46e024580cc9045123 to your computer and use it in GitHub Desktop.
// on Tap to reposition the cube
@objc func addCubeToSceneView(withGestureRecognizer recognizer: UIGestureRecognizer) {
let tapLocation = recognizer.location(in: sceneView)
switch recognizer.state {
case .began:
print("Object began to move")
let hitResults = self.sceneView.hitTest(tapLocation, options: nil)
if hitResults.isEmpty { return }
let hitResult = hitResults.first
if let node = hitResult?.node.parent?.parent?.parent {
self.object = node
}
case .changed:
print("Moving object position changed")
case .ended:
print("Done moving object")
let hitTestResults = sceneView.hitTest(tapLocation, types: .existingPlaneUsingExtent)
guard let hitTestResult = hitTestResults.first else {
DispatchQueue.main.async {
showTost(withMessage:AppConstant.Message.addFailure_on_surface ,fromViewController:self)
}
return }
planeNode.removeFromParentNode()
planeAnchorNode.removeFromParentNode()
shadowNode.removeFromParentNode()
let translation = hitTestResult.worldTransform.translation
let x = translation.x
let y = translation.y
let z = translation.z
object.position = SCNVector3(x,y,z)
sceneView.scene.rootNode.addChildNode(object)
shadowNode = shadowPlane()
shadowNode.position = SCNVector3(x,y - 0.1,z)
sceneView.scene.rootNode.addChildNode(shadowNode)
default:
break
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment