iosobjective-cios6uicollectionview

UICollectionView: how to detect when scrolling has stopped


I'm using a UICollectionView to scroll through a set of thumbnails quickly. Once scrolling ends, I'd like to display a larger hi-res version of the current thumbnail.

How can I detect when the user has completed scrolling? I do implement didEndDisplayingCell, but that only tells me when a particular cell has scrolled off; it doesn't tell me when the scroll motion actually completes.


Solution

  • NS_CLASS_AVAILABLE_IOS(6_0) @interface UICollectionView : UIScrollView
    

    UICollectionView is a subclass of UIScrollView. So if you have set the delegate and implemented UIScrollViewDelegate, you should be able to detect this the same way as UIScrollView.

    For eg:-

    Objective-C:

    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
    

    Swift:

    func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView)
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool)
    

    As per documentation, the above method should tell when the scroll view has ended decelerating the scrolling movement. Note that scrollViewDidEndDecelerating would not be called in some cases as mentioned in comments.