Reputation: 683
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];
Upvotes: 4
Views: 5537
Reputation: 135
Maybe you have disabled userInteraction. Did you check it? And define gestures as property to the class.
self.collectionView.setUserInteractionEnabled=true;
Upvotes: 2
Reputation: 2189
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.
Upvotes: 1
Reputation: 5121
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?
Upvotes: 0
Reputation: 291
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.
Upvotes: 1