Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect the scroll direction from the UICollectionView?

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

like image 497
sonifex Avatar asked Jun 29 '14 21:06

sonifex


1 Answers

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?

like image 85
Douglas Ferreira Avatar answered Sep 20 '22 15:09

Douglas Ferreira