I need to perform a specific action when the user swipes the uicollectionview. I built it in a way that each cell captures the full screen.
I tried those ways:
A. 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 the scrollVelocity
returns null. The method is being called.
B. UISwipeGestureRecognizer
In ViewDidLoad
of 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 followings 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 none are called.
What is wrong? I am developing for ios7