Add TapGestureRecognizer for the entire view except UICollectionView cells

I would like to add a TapGestureRecognizer to cover the entire screen of the UICollectionViewController, except for the UICollectionViewCell cells.

The closest I got

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

Problem. When I click on a cell, the prepareForSegue method is not called. It looks like UITapGestureRecognizer is covering the camera.

Which view in the UICollectionViewController is correct to attach the GestureRecognizer to keep the default click-to-execute function?

+4
source share
1 answer

Implement the gesture recognizer delegation 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; 
    }
}
+7

All Articles