Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 515
Bernd Avatar asked Mar 22 '23 01:03

Bernd


1 Answers

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; 
    }
}
like image 95
Adnan Aftab Avatar answered Mar 24 '23 15:03

Adnan Aftab