为多个CALayer设置动画,但不在同一时间空间 [英] animating multiple CALayers, but not in the same time space

查看:62
本文介绍了为多个CALayer设置动画,但不在同一时间空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个 CALayers ,我想制作一个新的 zPosition 的动画,每层的延迟略有延迟



每个动画应花费0.25秒并在上一个动画开始后0.05秒开始。在每个动画的结尾,将从图层树中删除该图层。



我已经成功地使用了 -animationDidStop:finished:委托方法,可在完成图层后将其删除,但我无法正确订购动画。



是否可以在其中安排动画

解决方案

我仍然想听听其他建议,但我想我已经解决了。 / p>

我现在正在创建特定的 CAAnimation 对象,并指定其 beginTime 属性。我之前做过,但是没有用,我终于意识到,要使 beginTime 属性得到兑现,必须将动画添加到 CAAnimationGroup



因此,我的代码如下:

  NSArray * layers = / *要动画消失的图层* / 
CGFloat startOffset = 0.01;

for(NSInteger index = 0; index< layers.count; index ++){
CALayer * layer = [layersObjectAtIndex:index];

CABasicAnimation * zoomOut = [CABasicAnimation animationWithKeyPath:@ zPosition];
zoomOut.toValue = [NSNumber numberWithDouble:400.0];
zoomOut.beginTime = index * startOffset;

CAAnimationGroup * group = [CAAnimationGroup动画];
group.animations = [NSArray arrayWithObject:zoomOut];
group.delegate =自我;

[layer addAnimation:group forKey:@ zoomAway];
}


I have several CALayers that I'm trying to animate to a new zPosition with each layer slightly delayed from the others.

Each animation should take 0.25 seconds and start 0.05 seconds after the previous animation started. At the end of each animation the layer will be removed from the layer tree.

I've successfully been using the -animationDidStop:finished: delegate method to remove my layers as they finish but I've not been able to order the animations properly.

Is it possible to schedule animations in this way, and how?

解决方案

I'd still like to hear others suggestions but I think I've solved this.

I'm now creating specific CAAnimation objects and specifying their beginTime property. I was doing this earlier and it wasn't working, what I finally realized is that for the beginTime property to be honored the animation has to be added to an CAAnimationGroup.

So, my code looks like this:

NSArray *layers = /* layers to be animated away */
CGFloat startOffset = 0.01;

for (NSInteger index = 0; index < layers.count; index++) {
    CALayer *layer = [layers objectAtIndex:index];

    CABasicAnimation *zoomOut = [CABasicAnimation animationWithKeyPath:@"zPosition"];
    zoomOut.toValue = [NSNumber numberWithDouble:400.0];
    zoomOut.beginTime = index * startOffset;

    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = [NSArray arrayWithObject:zoomOut];
    group.delegate = self;

    [layer addAnimation:group forKey:@"zoomAway"];
}

这篇关于为多个CALayer设置动画,但不在同一时间空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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