如何取代UICollectionView中的内置滑动手势识别器?

Mar*_*eIV 1 iphone objective-c ipad ios6 uicollectionview

我们正在构建一个利用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.
Run Code Online (Sandbox Code Playgroud)

基本上,我们只想在手势识别器出现故障时启用内置滑动手势.但是,我们不确定如何用"其他识别器必须首先失败"的逻辑取代内置的滑动手势识别器.

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

那么我们如何创建取代内置的识别器呢?

occ*_*lus 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];
Run Code Online (Sandbox Code Playgroud)

原始答案

看一下UITapGestureRecognizerDelegate,可以用两个手势识别器一次处理触摸:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {        
     return YES;
}
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅如下教程:

http://www.raywenderlich.com/6567/uigesturerecognizer-tutorial-in-ios-5-pinches-pans-and-more