CABasicAnimation当前经过的时间 [英] CABasicAnimation current elapsed time
问题描述
我只是从一个笨拙的while循环中重写了一个很大的动画(触发drawRect:x次),这是我无法弄清的最后一件事。
I just rewrote quite a big animation from a dumb while loop (firing drawRect: x times) and this is the last thing that I just can't figure out..
如何获取动画的当前经过时间?我知道如何获取当前的CFTimeInterval(是否可以暂停CABasicAnimation?):
How can I get the current elapsed time of my animation? I know how to get the current CFTimeInterval (Is there a way to pause a CABasicAnimation?):
CFTimeInterval currentTime = [self.multiplierLayer convertTime:CACurrentMediaTime() fromLayer:nil];
但是从动画开始的那一刻起,我该如何使用它来计算当前经过的时间呢?似乎beginTime始终为0.0,我必须设置动画开始的那一刻,然后从beginTime中提取currentTime吗?
But how can I use this to calculate the current elapsed time from the moment my animation started? It seems that beginTime is always 0.0, do I have to set that the moment my animation starts and then extract the currentTime from the beginTime?
很抱歉,很容易被忽略,我昨天才开始使用Core Animation。 :)
I'm sorry if it's something simple that I'm overlooking, I just started using Core Animation yesterday. :)
编辑:
设置beginTime不是不是的操作方式,实际上
Setting beginTime is not the way to do it, really at a loss here.
推荐答案
一种可能更简单的方法是,当您创建CABasicAnimation明确设置开始时间时,例如:
A possibly easier way to do what you want is when you create your CABasicAnimation explicitly set the starting time, like:
basicAnimation.beginTime = CACurrentMediaTime();
以后,您可以找出花费了多少时间:
Later on you can figure out how much time has elapsed with:
CFTimeInterval elapsedTime = CACurrentMediaTime() - basicAnimation.beginTime;
并通过以下方式获得百分比:
And get the percentage through with:
progress = elapsedTime / basicAnimation.duration;
(如果您有timeOffset或类似的东西,代码会稍微复杂一些。)
(The code will be slightly more complex if you have a timeOffset or something like that.)
这篇关于CABasicAnimation当前经过的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!