Search code examples
iosiphoneswiftuitapgesturerecognizer

Is there a way to have a two tap gesture recognizers with different number of taps


In my app, i need to run different functions on a tap gesture depending on how many times a user taps the image.

I have a tap gesture recognizer that requires for 1 tap.

My second tap gesture recognizer requires for 2 taps. However, if I tap it 2 times, both functions for each tap gesture are called.

Is there a way I can do this, so the two functions can be called separately and not interfere with each-other?

The problem: If i tap the image two times, it calls the goWatchVideo function and the onDoubleTap function all together. I want to avoid this.

// Gesture that requires 1 tap
let tapToWatch = UITapGestureRecognizer(target: self, action: #selector(goWatchVideo))
tapToWatch.numberOfTapsRequired = 1     
postVideoView.addGestureRecognizer(tapToWatch)


// Gesture that requires 2 taps  
let doubleLike = UITapGestureRecognizer(target: self, action:#selector(self.onDoubleTap))
doubleLike.numberOfTapsRequired = 2
postVideoView.addGestureRecognizer(doubleLike)

// Tried to set them up for failures
tapToWatch.require(toFail: doubleLike)
doubleLike.require(toFail: tapToWatch)

Solution

  • you need to make sure that double tap gesture is failed in order to initialize tapToWatch

    try this

    tapToWatch.require(toFail: doubleLike)