Search code examples
iosuiscrollviewuicollectionviewoverscroll

Allow UIScrollView to Scroll Past Content Without Bouncing


Overview

I'm building an app the allows the user to overscroll the main UICollectionView by a certain amount in order to load related content. The tactile experience of the overscroll should be the same as many other iOS apps that would cause the content on the current page to refresh, so I set collectionView.bounce = YES.

Once the user overscrolls by the designated amount, I want to move the current UICollectionView down and out of view, while simultaneously moving the new page down into view from the top of the device, very similar to the inter-article navigation in the Digg iPhone app.

I'm detecting the overscroll using this UIScrollViewDelegate method:

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    CGFloat offsetY = scrollView.contentOffset.y;
    CGFloat contentHeight = scrollView.contentSize.height;

    if (offsetY < -40){
        [self.delegate overscrolledUp];
    }

    if (offsetY - (contentHeight - scrollView.frame.size.height) > 40){
        [self.delegate overscrolledDown];
    }
}

Problem

The delegate takes care of moving the frames of these UICollectionViews, but my problem is that as the current page is moving down and out of view (or up and out of view if the user overscrolls down), the UICollectionView is also bouncing the scroll back to a contentOffset of (0,0). This results in a strange looking animation that I'd like to avoid. I want the user to be able to overscroll so an indicator is shown describing the new content they can see, but I don't want that overscroll to be reversed during the page-changing animation.

Question

Is there a way to prevent a UICollectionView/UIScrollView from returning its scroll to (0,0) after the user scrolls past the content?


Solution

  • Take a look at

    https://github.com/enormego/EGOTableViewPullRefresh

    It contains a pull to refresh tableview code, you can use it directly or as a source of inspiration.