Reputation: 1306
I've tried subclassing UICollectionView and overriding touchesBegan:withEvent:
and hitTest:WithEvent:
, and both of those methods trigger when I touch a cell. However, if I touch the space between the cells, nothing happens at all. Here's what I've created:
@interface WSImageGalleryCollectionView : UICollectionView
@end
..and..
@implementation WSImageGalleryCollectionView
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Touches began");
[super touchesBegan:touches withEvent:event];
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
NSLog(@"Hit test reached");
return [super hitTest:point withEvent:event];
}
@end
Note: gesture recognizers seem to have the exact same issue, which is why I tried going lower-level with touchesBegan.
Upvotes: 6
Views: 1843
Reputation: 271
You just need to set a view as background view that you can then add a gesture recognizer to like:
collectionView.backgroundView = [[UIView alloc] init];
[collectionView.backgroundView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnBackgroundRecognized)]];
Upvotes: 12
Reputation: 1306
In my case, the problem was that you can click on the background whenever it is "clear" colored. Otherwise simply setting a background color makes the background clickable. I don't quite know why this is, but the simple fix was to just give it a background color.
Edit: Actually, this may have had something to do with setting the "opaque" flag.
Upvotes: 0
Reputation: 69027
You must have got something else that is preventing touchesBegan
from firing.
Subclassing UICollectionView
and using that subclass inside of a UICollectionViewController
will allow you to detect in-between cells taps in touchesBegan
.
I have just tried it out by modifying Apple's CollectionView-simple sample.
Upvotes: 0