链接核心动画动画 [英] Chaining Core Animation animations

查看:84
本文介绍了链接核心动画动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪些是在核心动画上下文中链接动画的最优雅和模块化的方式?

Which is the most elegant and modular way to chain animation in a Core Animation context?

我的意思是做动画,只是在其他完成后开始(例如,更改位置,然后 opacity )..正常的做法是直接改变属性:

I mean to do animations that starts just when other finished (for example, changing position and then opacity).. normal approach is to directly change properties:

layer.position = new_point;
layer.opacity = 0.0f;

但这将同时进行。我想让一个人等待另一个。

but this will do them at the same time. I want to make one wait for the other.

不同对象的链接动画怎么样?我已经阅读过 CATransaction 使用像:

And what about chaining animations for different objects? I've read about CATransaction used like:

[CATransaction begin]
layer1.property = new_property;
[CATransaction begin]
layer2.property2 = new_property2;
[CATransaction commit];
[CATransaction commit];

但它似乎不工作。

推荐答案

您还可以使用动画分组并使用动画的beginTime字段。尝试这样:

You can also use animation grouping and use the beginTime field of the animation. Try something like this:

CABasicAnimation *posAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
[posAnimation setFromValue:[NSNumber numberWithFloat:0.0]];
[posAnimation setToValue:[NSNumber numberWithFloat:1.0]];
// Here's the important part
[posAnimation setDuration:10.0];
[posAnimation setBeginTime:0.0];

CABasicAnimation *borderWidthAnimation = [CABasicAnimation animationWithKeyPath:@"borderWidth"];
[borderWidthAnimation setFromValue:[NSNumber numberWithFloat:0.0]];
[borderWidthAnimation setToValue:[NSNumber numberWithFloat:1.0]];
// Here's the important part
[borderWidthAnimation setDuration:10.0];
[borderWidthAnimation setBeginTime:5.0];

CAAnimationGroup *group = [CAAnimationGroup animation];
[group setDuration:10.0];
[group setAnimations:[NSArray arrayWithObjects:posAnimation, borderWidthAnimation, nil]];

[layer addAnimation:group forKey:nil];

请注意,整个动画的持续时间为10秒。第一个从第二个0开始,第二个从5秒开始。

Notice that the duration of the entire animation is 10 seconds. The first one starts at second 0 and the second one starts at 5 seconds.

这篇关于链接核心动画动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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