简体   繁体   English

将TapGestureRecognizer添加到除UICollectionView单元格之外的整个视图

[英]Add a TapGestureRecognizer to whole view except UICollectionView cells

I'd like to add a TapGestureRecognizer to cover the whole screen of a UICollectionViewController except the UICollectionViewCell cells. 我想添加一个TapGestureRecognizer来覆盖除UICollectionViewCell单元格之外的UICollectionViewController的整个屏幕。

The closest I got was 我得到的最接近的是

-(void) viewDidLoad {
...
UITapGestureRecognizer *tapAnywhere = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(addBoard:)];
[self.collectionView addGestureRecognizer:tapAnywhere];
}

Problem: When I tap a cell the prepareForSegue method is not called. 问题:当我点击一个单元格时,不会调用prepareForSegue方法。 The UITapGestureRecognizer seems to cover the cell. UITapGestureRecognizer似乎涵盖了单元格。

Which View in a UICollectionViewController is the right one to attach the GestureRecognizer to retain its default cell "tap to segue" functionatlity? UICollectionViewController中的哪个View是连接GestureRecognizer以保留其默认单元格“tap to segue”功能的正确方法?

Implement Gesture Recognizer delegate method 实现Gesture Recognizer委托方法

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 
{
    if ([touch.view isKindOfClass:[UICollectionViewCell class]]) //It can work for any class you do not want to receive touch
    {
        return NO;
    }
    else 
    {
        return YES; 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM