ios UICollectionView检测滚动方向

Son*_*ler 13 objective-c uigesturerecognizer ios uipangesturerecognizer uicollectionview

我有一个collectionView.我想检测滚动方向.我有两种不同的动画风格,可以向下滚动并向上滚动.所以我必须学习滚动方向.

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");
Run Code Online (Sandbox Code Playgroud)

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

Dou*_*ira 29

试试这个:

在标题中的某处添加:

@property (nonatomic) CGFloat lastContentOffset;
Run Code Online (Sandbox Code Playgroud)

然后覆盖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;
}
Run Code Online (Sandbox Code Playgroud)

找到在UIScrollView查找滚动的方向?


Pat*_*gar 5

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

- (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");
    }
}
Run Code Online (Sandbox Code Playgroud)


gen*_*ius 5

斯威夫特 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
}
Run Code Online (Sandbox Code Playgroud)