Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
612 views
in Technique[技术] by (71.8m points)

iphone - How to make UIScrollView send scrollViewDidScroll messages during animations

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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) {}];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...