Ios – How to make UIScrollView send scrollViewDidScroll messages during animations

animationiosiphoneuiscrollview

When the user manually scrolls through my UIScrollView, the scrollViewDidScroll method of my delegate gets called repeatedly during the animation, with newly updated values of the contentOffset.
When I call "[scrollView setContentOffset:320 animated:YES", then the delegate method gets called in the same way.
I decided that the normal scroll speed is too fast for the user experience, so enclosed a "[scrollView setContentOffset:320]" in an "animatedWithDuration:" block, as Apple recommends in the UIView class reference.

But… now my scrollViewDidScroll method gets called only once at the beginning of the animation with the final value, and not during the animation anymore. I get the same effect when I use the old "beginAnimations:" methods instead.

So… anybody knows how to solve this?

By the way, the "setContentOffset" method of the UIScrollView shows the same behaviour. It used to be called during the animations, and now is called only once.

Best Answer

Thanks to Fichek's hint, I have gotten this to work. Like Fichek said, you don't get any notifications of changed properties during animation. So the trick is, to make sure that anything that depends on the changed property is also animated at the same time. You need to setup their animations in the same block as the original property. If you then set the "UIViewAnimationOptionAllowUserInteraction" on the animation, then any ongoing user interaction of the same properties will still work - and surprisingly well, I have to say.

For my concrete case - to keep a dragged view stationary, while the UIScrollView scrolls underneath - here is how I setup my animation:

[UIView animateWithDuration:0.5 delay:0
                    options:UIViewAnimationOptionAllowUserInteraction
                 animations:^{
    [theScrollView setContentOffset:offset];
    // compute newCenter from the new offset
    theDraggedView.center = newCenter;
} completion:^(BOOL finished) {}];
Related Topic