当用户滑动uicollectionview时,我需要执行特定的操作。我的设计是每个单元格占据整个屏幕。
我尝试了以下两种方法:
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");
}
}
但是scrollVelocity
返回null。该方法已被调用。
B. UISwipeGestureRecognizer
在我的UIViewController
的ViewDidLoad
中,我添加了以下内容,它同时代理了UICollectionViewDataSource
和UIGestureRecognizerDelegate
:
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];
还有在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;
}
但是没有一个被调用。
问题出在哪里?我的开发目标是iOS7。