如何比较iOS上的手势类型?

我在一个视图上有三种不同的手势,两种不同的类型。

首先是UITapGestureRecognizer ,另外两个是UILongPressGestureRecognizer

长按手势识别器具有不同的minimumPressDuration ,一个是0.15 ,另一个是0.50 ,所以我实现了他的跟随function,以便识别所有手势:

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

该function允许识别所有手势,但问题是只要识别出UITapGestureRecognizer ,也会识别UITapGestureRecognizer

所以,我想知道如何比较gestureRecognizer的类型

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

或者在检测到UITapGestureRecognizer时如何停止UITapGestureRecognizer ,因为UITapGestureRecognizer触发UITapGestureRecognizerUILongPressGestureRecognizer触发UILongPressGestureRecognizer

而不是在shouldRecognizeSimultaneouslyWithGestureRecognizer:对所有情况返回YES shouldRecognizeSimultaneouslyWithGestureRecognizer:如果你不想同时识别手势,你应该实际返回NO

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

但要完成您显然要完成的任务,我建议使用不同的UIGestureRecognizerDelegate方法 – gestureRecognizer:shouldBeRequiredToFailByGestureRecognizer: – 这样您就可以指定哪个手势在另一个之前被识别。 在这种情况下,由于您要在检测到UITapGestureRecognizer时停止UITapGestureRecognizer ,请尝试以下操作:

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

可以检查UIGestureRecognizer的类
例如:

 -(BOOL) gestureRecognizer: (UIGestureRecognize *) gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer: (UIGestureRecognizer *) otherGestureRecognizer { if ([gestureRecognizer isMemberOfClass: [UILongPressGestureRecognizer class]]) { //do stuff } //etc }