重复之前的CAKeyframeAnimation延迟 [英] CAKeyframeAnimation delay before repeating
问题描述
我有一个 CAKeyframeAnimation
动画,我想用 repeatCount = HUGE_VALF
永远重复。动画的持续时间是2秒,但我想在每个周期之前暂停3秒。
I have a CAKeyframeAnimation
animation that I would like to repeat forever using repeatCount = HUGE_VALF
. The animation's duration is 2 seconds, but I would like to have a 3 seconds pause before each cycle.
我能想到的唯一两种方法是:
The only 2 ways I can think of doing that are:
-
让整个动画持续5秒并添加额外的keyTimes和值,这样我就可以得到我在寻找的暂停5s动画的3s。这感觉有点哈哈。
Make the whole animation last 5 seconds and add extra keyTimes and values so that I get the pause I'm looking for during the last 3s of the 5s animation. This feels kinda hacky.
让动画只重复一次然后添加使用像 performSelector:afterDelay:2
再次运行动画,依此类推。这也很脏。也意味着我需要每5秒调用 addAnimation:
,我不确定它在性能方面是否是最佳的。
Have the animation only repeat once and then add use something like performSelector:afterDelay:2
to run the animation again, and so on and so on. This feels dirty as well. Also would mean that I need to call addAnimation:
every 5 seconds, which I'm not sure is optimal in terms of performance.
我可能缺少另一种选择吗?这两种方法中哪一种比另一种更好?
Is there another option I might be missing? Is one of those 2 methods better than the other?
推荐答案
通过转储Apple的 MKUserLocationView的动画
,我能够看到他们是如何做到的。事实证明这是 CAAnimationGroup
的用途。通过将2秒动画封装到5秒动画组中,您最终会得到2秒动画,然后是3秒延迟:
By dumping the animations of Apple's MKUserLocationView
, I was able to see how they were doing it. Turns out that this is what CAAnimationGroup
is for. By encapsulating a 2 seconds animation into a 5 seconds animation group, you'll end up with a 2 seconds animation followed by a 3 seconds delay:
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.duration = 5;
animationGroup.repeatCount = INFINITY;
CAMediaTimingFunction *easeOut = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
CABasicAnimation *pulseAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale.xy"];
pulseAnimation.fromValue = @0.0;
pulseAnimation.toValue = @1.0;
pulseAnimation.duration = 2;
pulseAnimation.timingFunction = easeOut;
animationGroup.animations = @[pulseAnimation];
[ringImageView.layer addAnimation:animationGroup forKey:@"pulse"];
这篇关于重复之前的CAKeyframeAnimation延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!