且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-01-25 15:19:02

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