4

I have three different gestures with two different types on one view.

First is a UITapGestureRecognizer and the two others are UILongPressGestureRecognizer.

The long press gesture recognizer have different minimumPressDuration, one is 0.15 and the other is 0.50, so to I implemented he following function so that all the gestures are recognized:

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer: (UIGestureRecognizer *) otherGestureRecognizer{
    return true;
}

The function does allow all the gestures to be recognized but the problem is whenever a UILongPressGestureRecognizer is recognized, a UITapGestureRecognizer is also recognized.

So, I want to know how can I compare the types of gestureRecognizer in

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer: (UIGestureRecognizer *) otherGestureRecognizer

or how to stop the UITapGestureRecognizer when UILongPressGestureRecognizer is detected because UITapGestureRecognizer is triggered whenever UILongPressGestureRecognizer is triggered.

rebello95
  • 8,486
  • 5
  • 44
  • 65
madtapper
  • 231
  • 2
  • 6
  • 11

3 Answers3

5

Instead of returning YES to all cases in shouldRecognizeSimultaneouslyWithGestureRecognizer:, if you don't want the gestures to be recognized simultaneously, you should actually return NO:

- (BOOL) gestureRecognizer: (UIGestureRecognize *) gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer: (UIGestureRecognizer *) otherGestureRecognizer {
   return NO;
}

But to accomplish what you're apparently trying to accomplish, I'd recommend using a different UIGestureRecognizerDelegate method instead -- gestureRecognizer:shouldBeRequiredToFailByGestureRecognizer: -- so that you can specify which gesture is recognized before the other. In this case, since you'd like to stop the UITapGestureRecognizer when a UILongPressGestureRecognizer is detected, try this:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

    // If the gesture recognizer is a UITapGestureRecongizer, but the other
    // gesture detected is a UILongPressGestureRecognizer, require the
    // UITapGestureRecognizer to fail.
    if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]] &&
        [otherGestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) {
       return YES;
    } else {
       return NO;
    }
}
Lyndsey Scott
  • 37,080
  • 10
  • 92
  • 128
1

Can check the class of the UIGestureRecognizer
For example:

-(BOOL) gestureRecognizer: (UIGestureRecognize *) gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer: (UIGestureRecognizer *) otherGestureRecognizer {
    if ([gestureRecognizer isMemberOfClass: [UILongPressGestureRecognizer class]]) {
       //do stuff
    }
//etc
}
shim
  • 9,289
  • 12
  • 69
  • 108
0

I converted the code for Swift 5 to add to this post. It should work as I pulled it off the Apple Documents.

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

I'm going to try this tomorrow as I've been struggling with UIScrollView and UISwipeGestureRecognizer working together. If I disable Scrolling, then the gesture works, but if I enable Scrolling (what I need), then gesture does not work. I want this feature for hiding and showing the top Navigation Bar and the bottom TabBar when scrolling up and down my scrollview. Then I'm going to try to get it to work with a UICollectionView on the next page in my app. Being new to coding, any suggestions would be appreciated if the above code won't be enough. I'll update this comment if it works or not.

David_2877
  • 257
  • 4
  • 7
  • Ok, I tried add these lines of code to my project, just copy and pasted into the class file of the UIViewController, outside of the ViewDidLoad area, and it worked! Super Happy! – David_2877 Feb 14 '20 at 13:50