当用户滑动 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
在ViewDidLoad
我的UIViewController
哪些代表中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 开发