Nuzhat Zari
Nuzhat Zari

Reputation: 3408

scrollViewDidEndScrollingAnimation not getting called

I want to perform some action when scrollview finish scrolling, so I wrote that acton in scrollViewDidEndScrollingAnimation delegate method. It is working fine when rect is not visible and scrollview scrolls to new rect. But when rect is already visible scrollViewDidEndScrollingAnimation method will not be called and so the method written inside scrollViewDidEndScrollingAnimation will not get called. But I want to call that action, can anyone knows how to call that method when scrollview finish scrolling?

Thanks in advance!

Upvotes: 2

Views: 3131

Answers (2)

DavidA
DavidA

Reputation: 3172

Implement both scrollViewDidEndDecelerating: and scrollViewDidEndDragging:

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // User lifted finger while scrolling
    [self doPostScrollAction];
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if(!decelerate)
    {
        // User lifted finger after stopping scrolling
        [self doPostScrollAction];
    }
}

Upvotes: 2

Max MacLeod
Max MacLeod

Reputation: 26652

I think this one catches a few people out. What actually happens is that scrollViewDidEndScrollingAnimation is only called if you explicitly invoke either the setContentOffset:animated: or the scrollRectToVisible:animated: methods.

As the UIScrollViewDelegate Protocol Reference states:

Discussion
The scroll view calls this method at the end of its implementations of the setContentOffset:animated: and scrollRectToVisible:animated: methods, but only if animations are requested.

So what to do? Well, let's not forget that typically the offsetting of content data in a scroll view is not animated. Rather, it is the result of continuously updating the contentOffset value. So, you could possibly trigger your method based upon a specific contentOffset using the scrollViewDidScroll: delegate method.

Alternatively, if it is something to be done after every scroll gesture - specifically, after the private UIScrollViewPanGestureRecognizer - then you could do it in scrollViewDidEndDecelerating::

Discussion
The scroll view calls this method when the scrolling movement comes to a halt. The decelerating property of UIScrollView controls deceleration.

Upvotes: 5

Related Questions