0

I can not detect the double-tap in screen? Do i need to turn on the property in the project?

So i try this, but it doesn't work:

let tapRec = UITapGestureRecognizer()

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



@objc func doubleTap(){
   print ("tap")
}
vhao91
  • 23
  • 4

1 Answers1

0

Try this:

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

And then your function:

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

Hope this helps!

Oscar
  • 404
  • 7
  • 19
  • Hi! Thank @OkiRules No it isn't. And i can not add the extension in the GameScene file, it shows an error: Declaration is only valid at file scope – vhao91 Mar 30 '19 at 11:38
  • Hi! It's working now. I write another function inside the GameViewController so it override the one in GameScene :) Thank you for your help! – vhao91 Mar 30 '19 at 14:52