如何在点击对象上运行操作而不是释放点击?

时间:2016-03-22 14:41:53

标签: swift2 uikit

这是船舶的简化场景包默认场景。点击船舶,释放和船舶旋转。如何修改程序,以便在您点击船舶时,操作开始?不用担心释放或保持水龙头。

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() }
}

1 个答案:

答案 0 :(得分:0)

手势识别器识别整个手势。对于触摸开始时发生的敲击和触摸结束事件发生在附近。只有在“点击手势”完成且两个事件都完成后,内置识别器才会触发。

如果您想要Touch Begin或Touch End上的不同行为,您必须自己处理低级别事件。要执行此操作,您可以创建自己的自定义UIGestureRecognizer,也可以创建自定义UIView并使用UIResponder中的方法,例如 touchesBegan(_:withEvent:)

Please see the Event Handling Guide for iOS