UICollectionView: how to detect when scrolling has stopped
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:-
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
As per documentation, the above method should tell when the scroll view has ended decelerating the scrolling movement.
Just to cover your bases you should implement both these UIScrollViewDelegate methods.In some cases there may not be a deceleration (and scrollViewDidEndDecelerating
would not be called), for e.g., the page is fully scrolled in place. In those case do your update right there in scrollViewDidEndDragging
.
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ if (!decelerate) { [self updateStuff]; }}- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ [self updateStuff];}
An important fact to note here.
This method gets called on User initiated scrolls (i.e a Pan gesture):
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
or in Swift:
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView)
On the other hand, this one gets called on all manually (programatically) initiated scrolls (like scrollRectToVisible
or scrollToItemAtIndexPath
):
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
or in Swift:
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView)