如何取代UICollectionView中的内置滑动手势识别器? [英] How can I supersede the built-in swipe gesture recognizer in UICollectionView?

查看:286
本文介绍了如何取代UICollectionView中的内置滑动手势识别器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We're building an app that takes advantage of the new UICollectionView in iOS 6. However, we need to implement a long-press behavior such that even if the user then moves their finger after, we want it ignored.

ie

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

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

Basically, we only want to allow the built-in swipe gesture to be enabled if our gesture recognizer fails. However, we're not sure how to supersede the built-in swipe gesture recognizers with that 'Other recognizer must fail first' logic.

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

We're not sure if we're allowed to simply walk the chain trying to find the UIScrollView and interrogate that as we don't know if that violates Apple's guidelines, and if I remember correctly, they actually warn against messing with their recognizers anyway.

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

So how can we create tap-to-hold recognizers that supersede the built in ones?

推荐答案

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

There's an example on page 36 of the UICollectionView programming guide:

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 ,它可以用来让两个手势识别器一次处理触摸:

Have a look at UITapGestureRecognizerDelegate, which can be used to allow two gesture recognizers to process touches at once:

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

有关详情,请参阅如下教程:

For more info, see a tutorial such as this:

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

这篇关于如何取代UICollectionView中的内置滑动手势识别器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆