我无法在屏幕上检测到双击吗?我需要打开项目中的属性吗?

所以我尝试了一下,但是没有用:

let tapRec = UITapGestureRecognizer()

tapRec.addTarget(self, action: #selector(GameScene.doubleTap))
tapRec.numberOfTapsRequired = 2
self.view!.addGestureRecognizer(tapRec)



@objc func doubleTap(){
   print ("tap")
}

最佳答案

尝试这个:

let tapRec = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap(_:)))
tapRec.delegate = self
tapRec.numberOfTapsRequired = 2
view.userInteractionEnabled = true
view.addGestureRecognizer(tapRec)


然后你的功能:

extension YourViewController: UIGestureRecognizerDelegate {
    func handleDoubleTap(_ gesture: UITapGestureRecognizer){
        print("doubletapped")
    }
}


希望这可以帮助!

10-08 17:25