4

Assuming standard configuration (up/down), I'd like to detect when a user is scrolling their UIColletionView up or down (which is subclass of UIScrollView and conforms to UIScrollViewDelegate). I don't see any information straight out of the delegate to detect this, although I may be over looking something.

If I know which direction the user is scrolling, then I can use these UICollectionViewDatasource methods to determine if I should load more data from the REST server, or purge information that I already have to manage fixed memory space.

// If scrolling down, section is appearing

- (UICollectionReusableView *)collectionView:(UICollectionView *)cv viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

// If scrolling down, last cell in section is disappearing

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{

// If scrolling up, last cell in section is appearing

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {

// If scrolling up, section is disappearing

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(UICollectionReusableView *)view forElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath{
Rui Peres
  • 25,741
  • 9
  • 87
  • 137
VaporwareWolf
  • 10,143
  • 10
  • 54
  • 80
  • Check this http://stackoverflow.com/questions/6217900/uiscrollview-reaching-the-bottom-of-the-scroll-view To know you have reached the bottom. – Rui Peres Apr 19 '13 at 19:39
  • I already know how to detect if I've reached the bottom. Thx. – VaporwareWolf Apr 19 '13 at 19:40
  • If you want to know if is going to up (or down): http://stackoverflow.com/questions/7706152/iphone-knowing-if-a-uiscrollview-reached-the-top-or-bottom – Rui Peres Apr 19 '13 at 19:41

2 Answers2

10

You can check UIScrollView's (which UICollectionView inherits from) panGestureRecognizer property and do something like this:

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

Swift 3.1:

let scrollVelocity = collectionView.panGestureRecognizer.velocityInView(collectionView.superview)
if (scrollVelocity.y > 0.0) {
    print("going down")
} else if (scrollVelocity.y < 0.0) {
    print("going up")
}
  • Great! Exactly what I was looking for. In case edits dont' make it, just realize that velocityInView returns a CGPoint struct, not a float. You need to check the y value if your collection view is a vertical scoller. – VaporwareWolf Apr 19 '13 at 20:32
  • @HCHogan I tried it but scrollView is null. What should be yourView? the UiCollectionView? – Dejell Oct 07 '13 at 18:17
0

Also you can use this:

CGPoint translation = [collectionView.panGestureRecognizer translationInView:collectionView.superview];
        if (translation.y > 0) {
            NSLog(@"DOWN");
        } else {

            NSLog(@"UP");
        }

More accurate

Saleh Albuga
  • 448
  • 5
  • 11