Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(iphone) force scrollViewDidEndDecelerating to be called after programmatically scrolling a view?

I animate the scroll with scrollRectToVisible:animated:

But scrollViewDidEndDecelerating is not getting called.

Is there a way to force the function to be called?

like image 692
eugene Avatar asked Jun 05 '11 07:06

eugene


4 Answers

scrollViewDidEndDecelerating won't be called for scrollRectToVisible or setContentOffset (i.e, scrolling programmatically). If you notice the declaration of this method in the header file it clearly mentions that it's "called on finger up as we are moving".

Now, to address your issue, scrollViewDidEndScrollingAnimation delegate will be called (for setContentOffset and scrollRectToVisible), which you can use.

like image 191
Tatvamasi Avatar answered Nov 05 '22 07:11

Tatvamasi


As you've found, scrollViewDidEndDecelerating isn't always called (if you moved a scroll view with your finger and brought it to a stop it wouldn't get called either).

Since scrollViewDidEndDecelerating is a delegate method you can force it to be called like this:

[[scrollView delegate] scrollViewDidEndDecelerating:scrollView];
like image 40
lxt Avatar answered Nov 05 '22 06:11

lxt


I solved it by calling scrollViewDidEndDecelerating from scrollViewDidEndScrollingAnimation

-(void) scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self scrollViewDidEndDecelerating:scrollView];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //your code
}
like image 5
Jiří Zahálka Avatar answered Nov 05 '22 07:11

Jiří Zahálka


Adding the code below fixed an issue in my case.

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    if !decelerate {
        scrollViewDidEndDecelerating(scrollView)
    }
}
like image 1
Konrad Siemczyk Avatar answered Nov 05 '22 06:11

Konrad Siemczyk