tl;dr:您需要在之前的完成后手动添加每个动画。
没有添加顺序动画的内置方法。您可以将每个动画的延迟设置为所有先前动画的总和,但我不建议这样做。
相反,我会创建所有动画,并按照它们应该运行的顺序将它们添加到可变数组(使用数组作为队列)。然后通过将自己设置为所有动画的动画代理,您可以在动画完成时获得animationDidStop:finished: 回调。
在该方法中,您将从数组中删除第一个动画(即下一个动画)并将其添加到图层中。由于您是委托人,您将在该动画完成时获得第二个动画,在这种情况下,animationDidStop:finished: 回调将再次运行,并且下一个动画将从可变数组中删除并添加到图层中。
一旦动画数组为空,所有动画都将运行。
一些示例代码可以帮助您入门。首先,您设置所有动画:
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
[animation setToValue:(id)[[UIColor redColor] CGColor]];
[animation setDuration:1.5];
[animation setDelegate:self];
[animation setValue:[view layer] forKey:@"layerToApplyAnimationTo"];
// Configure other animations the same way ...
[self setSequenceOfAnimations:[NSMutableArray arrayWithArray: @[ animation, animation1, animation2, animation3, animation4, animation5 ] ]];
// Start the chain of animations by adding the "next" (the first) animation
[self applyNextAnimation];
然后在委托回调中,您只需再次应用下一个动画
- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)finished {
[self applyNextAnimation];
}
- (void)applyNextAnimation {
// Finish when there are no more animations to run
if ([[self sequenceOfAnimations] count] == 0) return;
// Get the next animation and remove it from the "queue"
CAPropertyAnimation * nextAnimation = [[self sequenceOfAnimations] objectAtIndex:0];
[[self sequenceOfAnimations] removeObjectAtIndex:0];
// Get the layer and apply the animation
CALayer *layerToAnimate = [nextAnimation valueForKey:@"layerToApplyAnimationTo"];
[layerToAnimate addAnimation:nextAnimation forKey:nil];
}
我正在使用自定义键 layerToApplyAnimationTo,以便每个动画都知道它的层(它只适用于 setValue:forKey: 和 valueForKey:)。