4

I want to detect when a user swipes left or right in a collectionView in which one cell occupies the entire screen width. Is it possible to do without adding gesture recognizer. I have tried adding gesture recogniser, but it only works when we set scrollEnabled property of collectionView to NO.

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeRight:)];
swipeRight.delegate = self;
swipeRight.numberOfTouchesRequired = 1;
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];

UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeLeft:)];
swipeLeft.delegate = self;
swipeLeft.numberOfTouchesRequired = 1;
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];

[self.collectionView addGestureRecognizer:swipeLeft];
[self.collectionView addGestureRecognizer:swipeRight];
halfer
  • 18,701
  • 13
  • 79
  • 158
user694688
  • 593
  • 1
  • 15
  • 32

4 Answers4

2

Maybe you have disabled userInteraction. Did you check it? And define gestures as property to the class.

self.collectionView.setUserInteractionEnabled=true;
kocakmstf
  • 135
  • 12
1

Try to override scrollViewWillEndDragging:scrollView withVelocity:velocity targetContentOffset:targetContentOffset, since the collection view inherits from a scroll view. This way you should be able to evaluate the velocity parameter to determine the direction. Therefore you'll want to implement the UIScrollViewDelegate protocol.

STEX
  • 271
  • 2
  • 10
1

I add swipe gesture to collectionView. And it work fine.

So like Stephen Johnson said. I guess your cell have a scrollView. So It block gestures.

PowHu
  • 2,061
  • 14
  • 17
0

I am not sure I fully understand the context of your problem. I see that you have a collection view within a collection view. I am going to assume that the outer collection view scrolls vertical and the inner one scrolls horizontal. You will want to set up a fail dependency between the gesture recognizers for both collections.

//Data source for your outer collection view
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   ...
   UICollectionView* innerCollectionView = ...;

   [collectionView. panGestureRecognizer requireGestureRecognizerToFail:innerCollectionView. panGestureRecognizer];

   ...
}

If there is more going on than this with nested collection views can you give some more details?

Stephen Johnson
  • 5,195
  • 1
  • 21
  • 37