CAAnimationGroup
is meant for having multiple CAAnimation
subclasses being stacked together to form an animation, for instance, one animation can perform an scale, the other moves it around, while a third one can rotate it, it's not meant for managing multiple layers, but for having multiple overlaying animations.
That said, I think the easiest way to solve your issue, is to assign each CAAnimation
a beginTime
equivalent to the sum of the durations of all the previous ones, to illustrate:
for i in 0 ..< 20
{
let view : UIView = // Obtain/create the view...;
let bounce = CAKeyframeAnimation(keyPath: "transform.scale")
bounce.duration = 0.5;
bounce.beginTime = CACurrentMediaTime() + bounce.duration * CFTimeInterval(i);
// ...
view.layer.addAnimation(bounce, forKey:"anim.bounce")
}
Notice that everyone gets duration * i
, and the CACurrentMediaTime()
is a necessity when using the beginTime
property (it's basically a high-precision timestamp for "now", used in animations). The whole line could be interpreted as now + duration * i
.
Must be noted, that if a CAAnimation
s is added to a CAAnimationGroup
, then its beginTime
becomes relative to the group's begin time, so a value of 5.0
on an animation, would be 5.0
seconds after the whole group starts. In this case, you don't use the CACurrentMediaTime()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…