Swift2 如何在攻丝对象上运行操作而不是释放攻丝?

Swift2 如何在攻丝对象上运行操作而不是释放攻丝?,swift2,uikit,Swift2,Uikit,这里是一个简化的scenekit船的默认场景。轻触飞船,释放,飞船旋转。您如何修改程序,以便在点击船舶时启动操作?不用担心松开或按住水龙头 class GameViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let scene = SCNScene(named: "art.scnassets/ship.scn")! let cameraNode = SC

这里是一个简化的scenekit船的默认场景。轻触飞船,释放,飞船旋转。您如何修改程序,以便在点击船舶时启动操作?不用担心松开或按住水龙头

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

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

如果您希望在触摸开始或触摸结束时有不同的行为,则必须自己处理低级事件。为此,您可以创建自己的自定义
UIGestureRecognitor
,也可以创建自定义UIView并使用
UIResponder
中的方法,如

触摸已开始(:withEvent:)


我不清楚你在问什么。运行此代码的结果是什么?它与您希望发生的情况有何不同?此代码当前在从对象(船)释放点击时调用操作。我想知道启动点击时如何调用动作。您可以参考自定义手势并使用它。感谢您的帮助Scott和chancyWu。