Scrolling Detection in UICollectionView

I need to perform a specific action when a user views a uicollectionview. I built it so that each cell captures the entire screen.

I tried these ways:

but. scrollViewDidEndDecelerating

# pragma UIScrollView - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ NSLog(@"detecting scroll"); for (UICollectionViewCell *cell in [_servingTimesCollectionView visibleCells]) { NSIndexPath *indexPath = [_servingTimesCollectionView indexPathForCell:cell]; CGPoint scrollVelocity = [scrollView.panGestureRecognizer velocityInView:_servingTimesCollectionView]; if (scrollVelocity.x > 0.0f) NSLog(@"going right"); else if (scrollVelocity.x < 0.0f) NSLog(@"going left"); } } 

But scrollVelocity returns null. The method is called.

B. UISwipeGestureRecognizer

In the ViewDidLoad my UIViewController , which delegates to UICollectionViewDataSource and UIGestureRecognizerDelegate , I added:

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

and the following actions in the UiViewController:

 #pragma mark - UISwipeGestureRecognizer Action -(void)didSwipeRight: (UISwipeGestureRecognizer*) recognizer { NSLog(@"Swiped Right"); } -(void)didSwipeLeft: (UISwipeGestureRecognizer*) recognizer { NSLog(@"Swiped Left"); } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { NSLog(@"Asking permission"); return YES; } 

But no one is being called.

What's wrong? I am developing for ios7

+7
ios objective-c iphone uicollectionview uiswipegesturerecognizer
source share
1 answer

You do not set delegate gestures:

 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]; 
+8
source share

All Articles