0

I have a UIView A, and I added a UICollectionView B to A as a subView, so now view A is B's background view. Here didSelectItemAtIndexPath will get called normally at this time. But if I add a tap gesture recognizer to View A, then didSelectItemAtIndexPath won't be called.

  • Possible duplicate of [shouldReceiveTouch on UITableViewCellContentView](https://stackoverflow.com/questions/13275195/shouldreceivetouch-on-uitableviewcellcontentview) – Nirav D Aug 02 '18 at 06:54

2 Answers2

1

Few things you want to check:

  1. There is no self.collectionView.allowsSelection = NO. You want it to be YES.
  2. I have done this mistake in past where I accidentally tried to overwrite didDeselectItemAtIndexPath instead. Make sure you aren't like me.
  3. Use [tapGestureRecognizer setCancelsTouchesInView:NO] if it meets your purpose. It wouldn't send the cancel message and should do the trick.
Avi Dubey
  • 794
  • 6
  • 16
0
- (void)testTouch:(UITapGestureRecognizer*)gesture
{
    if ([self.collectionView hitTest:[gesture locationInView:self.collectionView] 
                           withEvent:nil])
    {
        NSLog(@"testTouch in collection");
    } 
    else
    {
        NSLog(@"testTouch in collection.superview");
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UITapGestureRecognizer *g = 
         [[UITapGestureRecognizer alloc] initWithTarget:self 
                                                 action:@selector(testTouch:)];
    g.cancelsTouchesInView = NO;
    [self.collectionView.superview addGestureRecognizer:g];
}
Roman Solodyashkin
  • 799
  • 12
  • 17