如何让 UIScrollView 在动画期间发送 scrollViewDidScroll 消息 [英] How to make UIScrollView send scrollViewDidScroll messages during animations

查看:24
本文介绍了如何让 UIScrollView 在动画期间发送 scrollViewDidScroll 消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户手动滚动我的 UIScrollView 时,我的委托的 scrollViewDidScroll 方法在动画过程中被重复调用,并使用新更新的 contentOffset 值.当我调用[scrollView setContentOffset:320 animation:YES"时,委托方法以相同的方式被调用.我认为正常的滚动速度对于用户体验来说太快了,因此将[scrollView setContentOffset:320]"包含在animatedWithDuration:"块中,正如 Apple 在 UIView 类参考中所推荐的那样.

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.

但是...现在我的 scrollViewDidScroll 方法仅在动画开始时使用最终值调用一次,而不再在动画期间调用.当我使用旧的beginAnimations:"方法时,我得到了同样的效果.

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?

顺便说一下,UIScrollView 的setContentOffset"方法显示了相同的行为.它曾经在动画期间被调用,现在只被调用一次.

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.

推荐答案

多亏了 Fichek 的提示,我才开始使用它.就像 Fichek 所说的那样,在动画过程中您不会收到任何更改属性的通知.所以诀窍是,确保任何依赖于更改的属性的东西也同时被动画化.您需要在与原始属性相同的块中设置它们的动画.如果您随后在动画上设置UIViewAnimationOptionAllowUserInteraction",那么相同属性的任何正在进行的用户交互仍然有效 - 我不得不说,效果出奇地好.

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.

对于我的具体情况 - 保持拖动视图静止,而 UIScrollView 在下方滚动 - 以下是我设置动画的方式:

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

这篇关于如何让 UIScrollView 在动画期间发送 scrollViewDidScroll 消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆