1

我们正在构建一个利用 iOS 6 中新的 UICollectionView 的应用程序。但是,我们需要实现长按行为,即使用户随后移动手指,我们也希望它被忽略。

IE

User touches the screen than instantly moves -> Swipe
User touches the screen, pauses, then moves -> Ignore swipe and wait for the release.

基本上,如果我们的手势识别器失败,我们只想允许启用内置的滑动手势。但是,我们不确定如何用“其他识别器必须首先失败”逻辑来取代内置的滑动手势识别器。

我们不确定我们是否被允许简单地走链条试图找到 UIScrollView 并询问它,因为我们不知道这是否违反了 Apple 的指导方针,如果我没记错的话,他们实际上警告不要弄乱他们的识别器.

那么,我们如何创建取代内置识别器的点击保持识别器呢?

4

2 回答 2

8

UICollectionView 编程指南的第 36 页上有一个示例:

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

于 2013-01-19T07:59:03.993 回答
0

在您UILongPressGestureRecognizer可能的状态进行新的转换之前,他会询问它的代表gestureRecognizerShouldBegin:。您可以使用该委托方法取消(强制进入失败状态)附加到同一视图的任何其他手势识别器。

您可以通过将以下内容实现为 new 的委托来做到这一点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用一根手指使用你的 new,同时用另一根手指滚动UICollectionView,你可以在同一个类中实现以下委托。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}
于 2014-08-19T08:18:49.693 回答