How to compare the types of gestures on iOS?

时光毁灭记忆、已成空白 提交于 2019-11-29 12:03:49

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;
    }
}

Can check the class of the UIGestureRecognizer
For example:

-(BOOL) gestureRecognizer: (UIGestureRecognize *) gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer: (UIGestureRecognizer *) otherGestureRecognizer {
    if ([gestureRecognizer isMemberOfClass: [UILongPressGestureRecognizer class]]) {
       //do stuff
    }
//etc
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!