15

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.

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

craft
  • 1,448
  • 1
  • 12
  • 26
sonifex
  • 260
  • 1
  • 2
  • 14

4 Answers4

30

Try this:

Add this somewhere in you header:

@property (nonatomic) CGFloat lastContentOffset;

Then override the scrollViewDidScroll: method:

#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?

Community
  • 1
  • 1
Douglas Ferreira
  • 2,209
  • 1
  • 24
  • 40
5

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");
    }
}
Patel Jigar
  • 1,971
  • 1
  • 21
  • 30
  • Dragging... Isn't it about using a finger to drag? Scroll even might happen without a finger of a user, isn't it? – iWheelBuy Feb 22 '20 at 17:41
4

Swift 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
}
karem_gohar
  • 316
  • 1
  • 3
  • 11
Gerges Eid
  • 851
  • 10
  • 14
2

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.

Robin Delaporte
  • 565
  • 5
  • 14