ios UICollectionView检测滚动方向

我有一个collectionView。 我想检测滚动方向。 我有一个两个不同的animation风格,向下滚动和向上滚动。 所以我必须学习滚动方向。

CGPoint scrollVelocity = [self.collectionView.panGestureRecognizer velocityInView:self.collectionView.superview]; if (scrollVelocity.y > 0.0f) NSLog(@"scroll up"); else if(scrollVelocity.y < 0.0f) NSLog(@"scroll down"); 

这只是在手指触摸工作。 不为我工作

尝试这个:

添加这个地方在你的头:

 @property (nonatomic) CGFloat lastContentOffset; 

然后重写scrollViewDidScroll:方法:

 #pragma mark - UIScrollViewDelegate - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (self.lastContentOffset > scrollView.contentOffset.y) { NSLog(@"Scrolling Up"); } else if (self.lastContentOffset < scrollView.contentOffset.y) { NSLog(@"Scrolling Down"); } self.lastContentOffset = scrollView.contentOffset.y; } 

find在UIScrollView滚动的方向?

这是滚动方向的最好方法,希望这可以帮助你

 - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset { CGPoint targetPoint = *targetContentOffset; CGPoint currentPoint = scrollView.contentOffset; if (targetPoint.y > currentPoint.y) { NSLog(@"up"); } else { NSLog(@"down"); } } 

我试图find一种方法来检测用户是否主要试图垂直或水平拉动scrollView。 我给你我的解决scheme,希望对任何人都有用:

 CGPoint _lastContentOffset; - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { _lastContentOffset = scrollView.contentOffset; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (ABS(_lastContentOffset.x - scrollView.contentOffset.x) < ABS(_lastContentOffset.y - scrollView.contentOffset.y)) { NSLog(@"Scrolled Vertically"); } else { NSLog(@"Scrolled Horizontally"); } } 

这项工作find我,我用这个来避免scrollView垂直滚动和相反时水平移动。