2

I know that there are posts out there that solves the identifying difference between single and double taps problem but they are all either outdated or in c++. So, I want to know how to identify the difference between single and double taps because every time I double tap the system thinks it is a tap. I did set the value of the numberOfTaps to 1 for single tap and 2 for double tap.

    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(respondToTapGesture(gesture:)))
    view.addGestureRecognizer(tap)

    tap.numberOfTapsRequired = 1

let doubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(respontToDoubleTapGesture(gesture:)))
    view.addGestureRecognizer(doubleTap)

    doubleTap.numberOfTapsRequired = 2
Andy
  • 169
  • 10
  • Do those outdated questions use `UITapGestureRecognizer`? My thinking is what's happening is your `tap` is tromping on your `doubleTap`. If so, what *might* work is adding code in your selectors to wait until you know it's a double tap. (I don't think using recognizers configured like this you can add code to only one gesture.) –  Jul 12 '17 at 23:43

2 Answers2

8

In order to recognize the taken action or to differentiate between the single tap and double, you need to fail the gesture, just add that code below

tap.require(toFail: doubleTap) 
Lamour
  • 3,002
  • 2
  • 16
  • 28
1

The problem is that you have two different recognizers which are trying to recognize the gesture.

This code will allow each of them to work:

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

You need to make sure you've declared the controller as a gesture recognition delegate:

class FromDB: UICollectionViewController, UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate {
}

you also need to require the single tap to fail for the double tap to kick in:

tap.require(toFail: doubleTap)
Mozahler
  • 4,958
  • 6
  • 36
  • 56