OGeek|极客世界-中国程序员成长平台

标题: core animation - animating multiple CALayers, but not in the same time space [打印本页]

作者: 菜鸟教程小白    时间: 2022-6-22 22:05
标题: core animation - animating multiple CALayers, but not in the same time space

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?



Best Answer-推荐答案


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





欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) Powered by Discuz! X3.4