(iphone) force scrollViewDidEndDecelerating to be

2019-03-12 20:33发布

问题:

I animate the scroll with scrollRectToVisible:animated:

But scrollViewDidEndDecelerating is not getting called.

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

回答1:

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.



回答2:

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];


回答3:

I solved it by calling scrollViewDidEndDecelerating from scrollViewDidEndScrollingAnimation

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

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //your code
}


回答4:

Adding the code below fixed an issue in my case.

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    if !decelerate {
        scrollViewDidEndDecelerating(scrollView)
    }
}