我们正在构建一个应用程序,在IOS 6采用新UICollectionView的优势。然而,我们需要实现长按行为,即使用户再经过移动他们的手指,我们希望它忽略。
即
User touches the screen than instantly moves -> Swipe
User touches the screen, pauses, then moves -> Ignore swipe and wait for the release.
基本上,我们只希望允许,如果我们的手势识别失败启用内置的滑动手势。 但是,我们不知道如何取代内置的滑动手势识别与“其他识别必须首先失败”的逻辑。
我们不知道,如果我们允许简单地走连锁试图找到的UIScrollView和询问,当我们不知道,如果违反苹果的指导方针,如果我没记错的话,他们实际上警惕反正他们识别搞乱。
那么,如何才能创建一个取代内置的那些轻击以保持识别?
还有的第36页上的例子UICollectionView编程指南 :
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
NSArray* recognizers = [self.collectionView gestureRecognizers];
// Make the default gesture recognizer wait until the custom one fails.
for (UIGestureRecognizer* aRecognizer in recognizers) {
if ([aRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
[aRecognizer requireGestureRecognizerToFail:tapGesture];
}
}
// Now add the gesture recognizer to the collection view.
tapGesture.numberOfTapsRequired = 2;
[self.collectionView addGestureRecognizer:tapGesture];
原来的答案
看看UITapGestureRecognizerDelegate
,这可以用来允许两个手势识别器可以一次处理触摸:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
欲了解更多信息,请参见教程像这样的:
http://www.raywenderlich.com/6567/uigesturerecognizer-tutorial-in-ios-5-pinches-pans-and-more
新的前UILongPressGestureRecognizer
从可能的状态transnitions,他将要求其委托gestureRecognizerShouldBegin:
可以使用该委托方法取消(力到故障状态)连接到相同的视图中的任何其他手势识别器。
您可以通过执行以下作为新的委托做UILongPressGestureRecognizer
:
@implementation DragPictogramGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
for (UIGestureRecognizer *gr in gestureRecognizer.view.gestureRecognizers) {
if ([gr isKindOfClass:[UILongPressGestureRecognizer class]] == NO) {
gr.enabled = NO;
gr.enabled = YES;
}
}
return YES;
}
@end
进一步,为了使用户能够使用新的UILongPressGestureRecognizer
用一个手指,并用另一个手指来滚动UICollectionView
的同时,可以实现在同一个类中的下列委托。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
文章来源: How can I supersede the built-in swipe gesture recognizer in UICollectionView?