如何在Spritekit中检测双击

时间:2019-03-30 06:46:53

标签: swift sprite-kit

我在touchesBegan中使用了这个touch.tapCount并尝试打印该值,但它从未返回2。

知道为什么吗?可以是1,3,,5,7,9,....但不能是2,4,6,8,10,...

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {


    if let touch = touches.first {
        //print(touch.tapCount)
        if ((touch.tapCount) == 1) {
            player.animatedPlayer()
            jump = true
        }
    }

}

1 个答案:

答案 0 :(得分:0)

您应该使用UITapGestureRecognizer处理tapEvent。 numberOfTapsRequired属性使此属性仅检测DoubleTap

 override func viewDidLoad() {
     super.viewDidLoad()
     var tap = UITapGestureRecognizer(target: self, action: 
     #selector(self.handleTapGesture(gesture:)))
     tap.numberOfTapsRequired = 2
     self.scene?.view?.addGestureRecognizer(tap)
 }

 @objc func handleTapGesture(gesture: UITapGestureRecognizer) -> Void {
    // do something here
 }

希望对您有所帮助!