0

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() }
}
mnuages
  • 13,049
  • 2
  • 23
  • 40
swl
  • 129
  • 2
  • 12
  • It is not clear to me what you are asking. What is the result of running this code and how is it different from what you want to happen? – Scott Thompson Mar 22 '16 at 21:27
  • This code currently calls the action when the tap is released from the object (the ship). I would like to know how to call the action when tap is initiated. – swl Mar 22 '16 at 23:21
  • you can refer [UITapGestureRecognizer - make it work on touch down, not touch up](http://stackoverflow.com/questions/15628133/uitapgesturerecognizer-make-it-work-on-touch-down-not-touch-up) to custom your gesture and use it. – chancyWu Mar 23 '16 at 08:53

1 Answers1

0

A gesture recognizer recognizes an entire gesture. For a tap that happens when the touch begin and touch end events happen in close proximity. The built in recognizer will only fire once the "tap gesture" is complete and both events are complete.

If you want different behavior on the Touch Begin or Touch End, you will have to handle the low-level events yourself. To do this you can either create your own, custom UIGestureRecognizeror you can create a custom UIView and use the methods in UIResponder like touchesBegan(_:withEvent:)

Please see the Event Handling Guide for iOS

Scott Thompson
  • 22,629
  • 4
  • 32
  • 34