Search code examples
iosobjective-cios6uicollectionview

Detecting pull-up or dragging of UICollectionView


I am trying to come up with a way to automatically load the next page of images when user scrolls to the bottom of the first page of images in UICollectionView. UITableView (iOS 6) has refreshControl for similar purpose ("Pull to refresh") which can be used to refresh, or something similar. Is there a similar option available for UICollectionView?

Is there a way to detect that a UICollectionView is being dragged beyond the last line of cells, or if it is displaying the footer (which is below the last line of cells). This action can then be used to trigger my method to load the next page of images.


Solution

  • Since collection views inherit from UIScrollView, you can implement scroll view delegate methods to detect the scroll motion. For instance, you can implement scrollViewDidScroll and use the contentOffset property to measure how far the user has scrolled down.

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        CGFloat offsetY = scrollView.contentOffset.y;
        CGFloat contentHeight = scrollView.contentSize.height;
        if (offsetY > contentHeight - scrollView.frame.size.height)
        {
            //start loading new images
        }
    }
    

    Hope this helps!