我在touchesStarted中使用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
}
}
}
最佳答案
您应该使用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
}
希望有用!