0

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)
vApp
  • 199
  • 3
  • 15
  • make sure this -> `postVideoView .isUserIntraction = true` – dahiya_boy Jan 02 '18 at 08:03
  • That's not the problem. I updated the description to further clarify the problem – vApp Jan 02 '18 at 08:07
  • https://stackoverflow.com/questions/8876202/uitapgesturerecognizer-single-tap-and-double-tap You should put the `require(toFail:)` only on the single tap with the double tap parameter. – Larme Jan 02 '18 at 08:13

2 Answers2

1

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

try this

tapToWatch.require(toFail: doubleLike)
rv7284
  • 1,002
  • 10
  • 24
0

I did not try it right now, but As far as I remember you need to set requiretofail only on the single recognizer as in this answer

Hons
  • 3,256
  • 3
  • 27
  • 48