ios 如何从 UICollectionView 检测滚动方向?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24480436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:10:35  来源:igfitidea点击:

How can I detect the scroll direction from the UICollectionView?

iosobjective-cuicollectionviewuigesturerecognizeruipangesturerecognizer

提问by Soner Güler

I have a UICollectionView. I want to detect scroll direction. I have a two different animation style for scroll down and scroll up. So I must learn scroll direction.

我有一个 UICollectionView。我想检测滚动方向。我有两种不同的动画样式用于向下滚动和向上滚动。所以我必须学习滚动方向。

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");

This is just work at finger touched. Not work for me

这只是手指触摸的工作。不适合我

回答by Douglas Ferreira

Try this:

尝试这个:

Add this somewhere in you header:

在标题中的某处添加:

@property (nonatomic) CGFloat lastContentOffset;

Then override the scrollViewDidScroll:method:

然后重写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;
}

Found in Finding the direction of scrolling in a UIScrollView?

找到 UIScrollView 中的滚动方向?

回答by Patel Jigar

this is the best way to get scroll direction, hope this helps you

这是获得滚动方向的最佳方式,希望对您有所帮助

- (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");
    }
}

回答by genius

Swift 4.2

斯威夫特 4.2

private var lastContentOffset: CGFloat = 0
func scrollViewDidScroll(_ scrollView: UIScrollView) {

    if lastContentOffset > scrollView.contentOffset.y && lastContentOffset < scrollView.contentSize.height - scrollView.frame.height {
        // move up
        print("move up")
        originalHeight ()
    } else if lastContentOffset < scrollView.contentOffset.y && scrollView.contentOffset.y > 0 {
        // move down
        print("move down")
        minimizeHeaderView()
    }

    // update the new position acquired
    lastContentOffset = scrollView.contentOffset.y
}

回答by Robin Delaporte

I was trying to find a way to detect if the user is mostly trying to pull vertically or horizontally the scrollView. I give you my solution, I hope it can be useful to anyone :

我试图找到一种方法来检测用户是否主要尝试垂直或水平拉动滚动视图。我给你我的解决方案,我希望它对任何人有用:

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");
    }

}

This work find for me and I use this to avoid the scrollView to move horizontally when scrolling vertically and opposite.

这项工作适合我,我使用它来避免在垂直和相反滚动时 scrollView 水平移动。