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];

      

+3


source to share


4 answers


You may have disabled userInteraction. Have you checked it out? And define gestures as a property for the class.



self.collectionView.setUserInteractionEnabled=true;

      

+2


source


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

.



+1


source


I add swipe gesture to collectionView. And it works great.

Just like Stephen Johnson said. I think your camera has a scrollView. Therefore, it blocks gestures.

+1


source


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?

0


source







All Articles