Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
832 views
in Technique[技术] by (71.8m points)

objective c - How to compare the types of gestures on iOS?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...