如何调用scrollViewDidScroll:与UIScrollView的方式相同,但在自定义动画? [英] How to call scrollViewDidScroll: the same way UIScrollView does, but during custom animation?

查看:215
本文介绍了如何调用scrollViewDidScroll:与UIScrollView的方式相同,但在自定义动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常大的水平滚动UIScrollView,它是重用其子视图(移动和更新他们,当他们在可见区域,类似于UITableView是重用单元格)。这依赖于scrollViewDidScroll:委托调用,它给我实际的contentOffset,这里我决定什么时候重用特定的子视图。到目前为止这么好。

I have a very large horizontally scrolling UIScrollView which is reusing its subviews (moves and updates them when they are out of visible area, similar like UITableView is reusing cells). This relies on scrollViewDidScroll: delegate call, which gives me actual contentOffset, and here I decide when to reuse particular subview. So far so good.

有时我需要改变contentOffset的程序,但用自定义动画(惯性和反弹到最终位置)。我可以很容易地使用核心动画。

Sometimes I need to change contentOffset programatically, but with custom animation (inertia and bounce back to the final position). I can do this quite easily using core animation.

问题是,在自定义动画期间scrollViewDidScroll:委托方法不调用 - >我必须手动所以子视图重用工作。我试图调用它与定时器每次0.02秒。现在有两个问题:

The problem is, that during custom animation scrollViewDidScroll: delegate method is not called -> I must do it manually so that subviews reusing works. I tried to call it with timer firing each 0.02 sec. Now there are two problems:


  1. 我必须使用[[_scrollView.layer presentationLayer] bounds]获得UIScrollView contentOffset] .origin.x因为在动画期间,正常的_scrollView.contentOffset不会改变。

  1. I must get UIScrollView contentOffset using [[_scrollView.layer presentationLayer] bounds].origin.x because during animation normal _scrollView.contentOffset does not change.

然而,presentationLayer的信息不足以进行精确同步 -

However info from presentationLayer is not sufficient for exact syncing - sometimes it is bit late.

问题是新的contentOffset远离当前位置。它看起来像内置的UIScrollView动画是CAKeyframeAnimation,和scrollViewDidScroll应该在关键帧位置上调用。但我不能得到这些。

problem is when the new contentOffset is far from current position. It looks like built-in UIScrollView animation is CAKeyframeAnimation, and scrollViewDidScroll should is called on key frames positions. But I am not able to get these.

如果我依靠与关键帧同步的计时器,视图会在错误的地方重复使用,

If I rely on timer which is not synced with key frames, views are reused on wrong places and I can not see them at all during animation.

任何人都可以了解UIScrollView在setContentOffset中调用scrollViewDidScroll:X animated:YES?是否有可能重现它而不会破坏应用商店的规则?

推荐答案

首先, NSTimer 延迟0.02秒 - 这不是什么计时器是应该的。尝试使用 CADisplayLink ,它会每帧触发一次。

First of all, I wouldn't use an NSTimer with a delay of 0.02 secs - that's not what timers are supposed for. Try using a CADisplayLink, it fires once per frame.

在回调方法中,您可以(如果您的自定义动画正在运行)运行自己的物理代码,并分别调用-setContentOffset:animated:。这甚至可以让你按指数缓解CA不会让你失望。

In your callback method you can - if your custom animation is running - run your own physics code and call -setContentOffset:animated: respectively. This even allows you to ease exponentially which CA won't let you.

这篇关于如何调用scrollViewDidScroll:与UIScrollView的方式相同,但在自定义动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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