(iphone) force scrollViewDidEndDecelerating to be called after programmatically scrolling a view?
I animate the scroll with scrollRectToVisible:animated:
But scrollViewDidEndDecelerating
is not getting c开发者_如何学运维alled.
Is there a way to force the function to be called?
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.
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];
I solved it by calling scrollViewDidEndDecelerating from scrollViewDidEndScrollingAnimation
-(void) scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
[self scrollViewDidEndDecelerating:scrollView];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//your code
}
Adding the code below fixed an issue in my case.
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if !decelerate {
scrollViewDidEndDecelerating(scrollView)
}
}
Use
scrollView.delegate
.For very smooth animation of the view, you should add a delegate function.
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate, decelerate: Bool) {
if decelerate == false {
scrollViewDidEndDecelerating(scrollView)
}
}
精彩评论