Fast tap gestures in Swift 4 and iOS 11

2.9k Views Asked by At

I just made a simple app to try out any type of gestures. I got to the tap gesture. So I thought, what if I made a fast tap game kind of application that counts the amount of taps the user performed. But soon enough I ran into some issues.

It did not count all the taps. If I began to tap as fast as possible, but it skipped taps.

The idea is I programmatically created a view in the superview and added a tapGestureRecognizer on the view. And simply put the “taps” into a label in the app.

It seems to fail to receive a system gesture on time.

The code:

let tap = UITapGestureRecognizer(target: self, action: #selector(tapped(sender:)));
    tap.numberOfTapsRequired = 1;
    animationView.isUserInteractionEnabled = true;
    animationView.addGestureRecognizer(tap);

The function:

@objc func tapped (sender :UITapGestureRecognizer) {
   self.counter += 1;
   self.lblScore.text = String(self.counter);
}

I have an animationView that I made "tappable" and it works. Every time I tap the animationView it increments the value of 'counter' that works! but every time I get this error if I tap too fast:

<_UISystemGestureGateGestureRecognizer: 0x1c01c4b00>: Gesture: Failed to receive system gesture state notification before next touch
1

There are 1 best solutions below

0
P.J.Radadiya On

Create Gesture :

let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture(_:)))
panGesture.delegate = self
Button.addGestureRecognizer(panGesture)

let longTapGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongTapGesture(_:)))
Button.addGestureRecognizer(longTapGesture)

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture(_:)))
Button.addGestureRecognizer(tapGesture)

let rotateGesture = UIRotationGestureRecognizer(target: self, action: #selector(handleRotateGesture(_:)))
rotateGesture.delegate = self
Button.addGestureRecognizer(rotateGesture)

let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(handlePinchGesture(_:)))
pinchGesture.delegate = self
Button.addGestureRecognizer(pinchGesture)    

Gesture click Event :

extension AddTextVC: UIGestureRecognizerDelegate {
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }

    @IBAction func handlePanGesture(_ recognizer: UIPanGestureRecognizer) {

    }

    @IBAction func handlePinchGesture(_ recognizer: UIPinchGestureRecognizer) {

    }

    @IBAction func handleRotateGesture(_ recognizer: UIRotationGestureRecognizer) 
    {

    }

    @IBAction func handleTapGesture(_ recognizer: UITapGestureRecognizer) {

    }

    @IBAction func handleLongTapGesture(_ recognizer: UITapGestureRecognizer) {

    }
}