Scrolling UICollectionView
I want to detect when a user searches left or right in a collection where one cell takes up the entire width of the screen. Is it possible to do without adding a gesture recognizer. I tried to add a gesture recognizer, but it only works when we set the scrollEnabled property of the 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];
source to share
Try to override scrollViewWillEndDragging:scrollView withVelocity:velocity targetContentOffset:targetContentOffset
as collection view inherits from scroll view. Thus, you should be able to estimate the speed parameter to determine the direction. Therefore, you will want to implement the protocol UIScrollViewDelegate
.
source to share
I'm not sure I fully understand the context of your problem. I can see that you have a collection view as a collection. I'm going to assume that the scrolls collection's outer appearance is vertical and the inner one is horizontal. You want to set up a failure dependency between 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 with nested collection views, can you provide more details?
source to share