简体   繁体   中英

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.

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. The UITapGestureRecognizer seems to cover the cell.

Which View in a UICollectionViewController is the right one to attach the GestureRecognizer to retain its default cell "tap to segue" functionatlity?

Implement Gesture Recognizer delegate method

- (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; 
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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