如何在iOS应用程序中链接不同的CAAnimation [英] How to chain different CAAnimation in an iOS application

查看:66
本文介绍了如何在iOS应用程序中链接不同的CAAnimation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要链接动画,CABasicAnimation或CAAnimationGroup,但是我不知道该怎么做,唯一要做的是所有动画都在同一层同时执行。

I need to chain animations, CABasicAnimation or CAAnimationGroup but I don't know how to do it, the only that I do is that all the animation execute at the same time for the same layer.

我该怎么做?

例如,一个其内容设置为汽车图像的图层:

For example, a layer with its contents set to a car image:

第一:向右移X点

第二:向左旋转90ª

第3次:移动X点

第4次:缩放图层

所有这些动画必须以保密方式执行方式,但是我做不到:S

All this animations must be executed in a secuencial way, but I can't do it :S

BTW:我不是英语,对不起,如果我在语法上犯了一些错误:D

BTW: I am not english, sorry if I made some mistakes in my grammar :D

推荐答案

tl; dr::您需要在先前完成后手动添加每个动画。

tl;dr: You need to manually add each animation after the previous finishes.

没有内置的方式来添加连续动画。您可以将每个动画的延迟设置为以前所有动画的总和,但我不建议这样做。

There is no built in way to add sequential animations. You could set the delay of each animation to be the sum of all previous animations but I wouldn't recommend it.

相反,我将创建所有动画并将它们按应该运行的顺序添加到可变数组中(使用数组作为队列)。然后,通过将自己设置为所有动画的动画委托,您可以在动画结束时获取 animationDidStop:finished:回调。

Instead I would create all the animations and add them to a mutable array (using the array as a queue) in the order they are supposed to run. Then by setting yourself as the animations delegate to all the animations you can get the animationDidStop:finished: callback whenever an animation finishes.

在该方法中,您将从数组中删除第一个动画(即下一个动画),并将其添加到图层中。由于您是委托人,当第二个动画结束时,您将得到第二个动画,在这种情况下, animationDidStop:finished:回调将再次运行,并且下一个动画将从可变数组中删除并添加到该层。

In that method you will remove the first animation (meaning the next animation) from the array and add it to the layer. Since you are the delegate you will get a second animation when that one finishes in which case the animationDidStop:finished: callback will run again and the next animation is removed from the mutable array and added to the layer.

一旦动画数组为空,所有动画都将运行。

Once the array of animations is empty, all animations will have run.

一些示例代码可以帮助您入门。首先,您设置所有动画:

Some sample code to get you started. First you set up all your animations:

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];

然后在委托回调中,您只需再次应用下一个动画

Then in the delegate callback you simply apply the next animation again

- (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:起作用)。

I'm using a custom key layerToApplyAnimationTo so that each animation knows its layer (it works just by setValue:forKey: and valueForKey:).

这篇关于如何在iOS应用程序中链接不同的CAAnimation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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