如何比较iOS上的手势类型? [英] How to compare the types of gestures on iOS?
问题描述
我在一个视图上有三种不同的手势,两种不同的类型。
I have three different gestures with two different types on one view.
首先是 UITapGestureRecognizer
和另外两个是 UILongPressGestureRecognizer
。
长按手势识别器有不同的 minimumPressDuration
,一个是 0.15
,另一个是 0.50
,所以我实现了以下功能,以便所有手势被识别:
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;
}
该功能允许识别所有手势但问题是每当a UILongPressGestureRecognizer
被识别, UITapGestureRecognizer
也被识别。
The function does allow all the gestures to be recognized but the problem is whenever a UILongPressGestureRecognizer
is recognized, a UITapGestureRecognizer
is also recognized.
所以,我想知道如何比较中的gestureRecognizer类型
So, I want to know how can I compare the types of gestureRecognizer in
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer: (UIGestureRecognizer *) otherGestureRecognizer
或如何停止 UITapGestureRecognizer
当检测到 UILongPressGestureRecognizer
时,因为只要 UILongPressGestureRecognizer UILongGressureRecognizer
就会触发 code>被触发。
or how to stop the UITapGestureRecognizer
when UILongPressGestureRecognizer
is detected because UITapGestureRecognizer
is triggered whenever UILongPressGestureRecognizer
is triggered.
推荐答案
而不是返回 YES
对于中的所有情况shouldRecognizeSimultaneouslyWithGestureRecognizer:
,如果你不想要手势要同时被识别,你应该实际返回否
:
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;
}
但为了完成你显然想要完成的事情,我建议使用不同的 UIGestureRecognizerDelegate
方法 - gestureRecognizer:shouldBeRequiredToFailByGestureRecognizer:
- 这样您就可以指定之前识别的手势另一个。在这种情况下,由于您要在检测到 UILongPressGestureRecognizer
时停止 UITapGestureRecognizer
,请尝试以下操作:
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;
}
}
这篇关于如何比较iOS上的手势类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!