4

I'm using an UICollectionView added on top of a view with a single tap gesture recogniser. The CollectionView makes use of custom cells without any subviews. The delegate's method

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

is only called when selecting the cell with two instead of one finger or when doing a LONG press using a single finger.

I'm not accidentally overriding

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath.

I read through all similar questions which were all solved by removing some kind of view or gesture recogniser.

Any ideas what's wrong here?

Christoph
  • 1,965
  • 16
  • 35
  • 1
    I'm feeling uder water rocks, can you share a simple project with it behaviour? – Bimawa Apr 11 '17 at 11:07
  • post your for selection please – Devang Tandel Apr 11 '17 at 11:07
  • did you tried without `didDeselectItemAtIndexPath ` ? – Jack Apr 11 '17 at 11:09
  • self.collectionView.allowsMultipleSelection = false try this – Sivajee Battina Apr 11 '17 at 11:11
  • This link may helpful: http://stackoverflow.com/questions/16444688/uicollectionview-only-calling-didselectitematindexpath-if-user-double-taps-will – Sivajee Battina Apr 11 '17 at 11:11
  • @Janesh yes, tried with and without implementing didDeselectItemAtIndexPath – Christoph Apr 11 '17 at 11:18
  • @SivajeeBattina Tried this, still same behaviour. This property allows you to select multiple cells at once and does not relate to the number of fingers AFAIK. Also read through similar questions trying to be a good stack overflower ;) – Christoph Apr 11 '17 at 11:19
  • @Christoph test replacing `if(indexPath.row == self.viewModel.randomListViewModel.viewModels.count){ [self displayRandomLocations]; }` With `NSLog` What you seen ? – Jack Apr 11 '17 at 11:21
  • @Janesh I set my breakpoint in the first line to verify, it gets called whenever a hit the last cell with 2 fingers but not one, the condition is fine in this case. Thanks for looking into this :) – Christoph Apr 11 '17 at 11:22
  • Edited my question to reflect that in fact it is also working when using a long press with a single finger. Is this some how a behaviour that can be achieved using an UICollectionView? – Christoph Apr 11 '17 at 11:32
  • Thanks for your efforts, resolved the issue as you can see in my answer. Still have no explanation why it behaves like this. – Christoph Apr 11 '17 at 11:48

3 Answers3

5

The solution: The collection view was added on top of a view containing a single-tap gesture recogniser. This some how caused this behaviour. I removed the recogniser from the collection views parent view and it works.

Feel free to explain why this is expected behaviour. I would have argued that the top most view (CollectionView) handles the touches before they are passed to the view behind.

Christoph
  • 1,965
  • 16
  • 35
2

An alternative to removing the gesture completely is to set it so that it doesn't cancel touches in view.

UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(someSelector)];
tapRecognizer.cancelsTouchesInView = NO;
tapRecognizer.delaysTouchesBegan = NO;
tapRecognizer.delaysTouchesEnded = NO;
[self.view addGestureRecognizer:tapRecognizer];
Sandy Chapman
  • 11,133
  • 3
  • 58
  • 67
0

Based on Christoph solution; I've come up with this code, which is removing all the gesture recognisers in my parent view.

for (UIGestureRecognizer *recognizer in self.view.gestureRecognizers) {
    [self.view removeGestureRecognizer:recognizer];
}