4
votes

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];
4
why you want to use swipe gesture? - ChintaN -Maddy- Ramani
I want to do some processing on detecting whether it is left or right swipe - user694688
Does the collection view scroll vertical or horizontal? Is the real question just that you want to detect left/right swipes on a collection view or that you really don't want a gesture recognizer to do it? - Stephen Johnson
horizontal, i don't want a gesture recogniser as i have this collection view as a cell inside another collection view. - user694688
UICollectionView is one type of scrollview so you can override scrollViewDidScroll to check the direction of scrolling. Check this answer - [stackoverflow.com/a/4073028/900024] - Pratik Mistry

4 Answers

2
votes

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

self.collectionView.setUserInteractionEnabled=true;
1
votes

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.

1
votes

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.

0
votes

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?