How to compare the types of gestures on iOS?

后端 未结 3 1807
囚心锁ツ
囚心锁ツ 2020-12-19 15:28

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

First is a UITapGestureRecognizer and the two others are UILongPressGestureR

相关标签:
3条回答
  • 2020-12-19 16:10

    Can check the class of the UIGestureRecognizer
    For example:

    -(BOOL) gestureRecognizer: (UIGestureRecognize *) gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer: (UIGestureRecognizer *) otherGestureRecognizer {
        if ([gestureRecognizer isMemberOfClass: [UILongPressGestureRecognizer class]]) {
           //do stuff
        }
    //etc
    }
    
    0 讨论(0)
  • 2020-12-19 16:16

    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.

    0 讨论(0)
  • 2020-12-19 16:28

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题