Here is a simplified scenekit default scene with the ship. Tap the ship, release, and the ship spins. How do you modify program so that when you tap the ship, the action starts? No worry about releasing or holding tap.
class GameViewController: UIViewController {
override func viewDidLoad() { super.viewDidLoad()
let scene = SCNScene(named: "art.scnassets/ship.scn")!
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
scene.rootNode.addChildNode(cameraNode)
cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)
let lightNode = SCNNode()
lightNode.light = SCNLight()
lightNode.light!.type = SCNLightTypeOmni
lightNode.position = SCNVector3(x: 0, y: 10, z: 10)
scene.rootNode.addChildNode(lightNode)
let ambientLightNode = SCNNode()
ambientLightNode.light = SCNLight()
ambientLightNode.light!.type = SCNLightTypeAmbient
ambientLightNode.light!.color = UIColor.darkGrayColor()
scene.rootNode.addChildNode(ambientLightNode)
let scnView = self.view as! SCNView
scnView.scene = scene
scnView.allowsCameraControl = true
scnView.showsStatistics = true
scnView.backgroundColor = UIColor.blackColor()
let tapGesture = UITapGestureRecognizer(target: self, action: "handleTap:")
scnView.addGestureRecognizer(tapGesture)
}
func handleTap(gestureRecognize: UIGestureRecognizer) {
let scnView = self.view as! SCNView
// the ship
let ship = scnView.scene!.rootNode.childNodeWithName("ship", recursively: true)!
// the action
let rotateY = SCNAction.repeatActionForever(SCNAction.rotateByX(0, y: 2, z: 0, duration: 1))
let point = gestureRecognize.locationInView(scnView)
let hitResults = scnView.hitTest(point, options: nil)
if hitResults.count > 0 {
let result: AnyObject! = hitResults[0]
// the call
if result.node!.name!.hasPrefix("ship") {
ship.runAction(rotateY)
}
}
}
override func shouldAutorotate() -> Bool { return true }
override func prefersStatusBarHidden() -> Bool { return true }
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone { return .AllButUpsideDown }
else { return .All }
}
override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() }
}