How to receive touches on a UICollectionView in the blank space around all cells

ios, objective-c, swift, uicollectionview, uigesturerecognizer

Solution

I've managed to fix this problem by using a `UITapGestureRecognizer` on the `UICollectionView` `backgroundView`. It's in Swift, but the idea is clear:

self.tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTap:")
self.tapGestureRecognizer.delegate = self

self.collectionView.backgroundView = UIView(frame:self.collectionView.bounds)
self.collectionView.backgroundView!.addGestureRecognizer(tapGestureRecognizer)

And the callback:

func handleTap(recognizer: UITapGestureRecognizer) {
    // Handle the tap gesture
}

Problem

I have a `UICollectionView` that has different items in it. When I tap on an item, I use: ``` -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath ``` to figure out what was touched and then basically set the alpha of that view to 0 to hide it. That all works fine. Now what I would like to do is when you tap on the white space surrounding all of the `UICollectionViewCell`s all of the views then appear again. I am having trouble finding a method that will allow me to know when the white space around the cells has been touched. Is there a good way to do that? I have tried setting up a gesture recognizer, but when I do that, my method ``` -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath ``` isn't called. Is there some way to to just implement the gesture recognizer and from there determine if a cell was tapped and if so hide that cell, else show all the hidden cells? Thanks.

Original source