如何同时成功为多个CALayer制作动画?

时间:2011-07-29 08:17:27

标签: iphone core-animation caanimation cakeyframeanimation catransaction

我成功动画了单个图层,以改变其在屏幕上任意路径上的位置。我现在正试图多次复制这个动画,以给出弯曲角落的东西的错觉。我将代码放在CATransaction中,并将其放在循环中,为循环的每次迭代递增起始位置,然后在循环后提交CATransaction。我看到的效果是相同的,如果代码不在循环中(也就是说,只有一个图层被动画化),那么在动画结束时所有图层都会出现(在我的委托在animationDidStop动画结束时删除它们之前) )

我写的代码如下:

NSArray* path = [board caclulatePath:s];
    [CATransaction begin];
    [CATransaction setValue:[NSNumber numberWithFloat:([path count] * 0.25)] forKey:kCATransactionAnimationDuration];
    for (int i = 0; i < 20; i++)
    {
        CALayer* laserLayer = [CALayer layer];
        laserLayer.bounds = CGRectMake(s.frame.origin.x, s.frame.origin.y + (10*i), 20, 10);
        laserLayer.position = CGPointMake(s.frame.origin.x + (s.frame.size.width / 2), s.frame.origin.y + (s.frame.size.height / 2) + (10*i));
        laserLayer.contents = (id)[UIImage imageNamed:@"Laser.png"].CGImage;
        [self.layer addSublayer:laserLayer];

        CAKeyframeAnimation* anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        anim.values = path;
        anim.duration = ([path count] * 0.25);
        anim.removedOnCompletion = NO;
        anim.delegate = self;
        anim.rotationMode = kCAAnimationRotateAuto;
        [anim setValue:@"Fire" forKey:@"Action"];
        [anim setValue:laserLayer forKey:@"Layer"];
        [laserLayer addAnimation:anim forKey:nil];
    }
    [CATransaction commit];

其中[board caclulatePath:s]返回表示CGPoints的NSValues的NSArray *。

我怎样才能达到我所追求的效果(哪个是相同路径下的laser.png的多个副本)? [Laser.png是一个20px x 20px的红色正方形];

2 个答案:

答案 0 :(得分:2)

看看CAAnimationGroup。我认为它可能适合你的问题。

答案 1 :(得分:2)

实际问题是每个层WAS同时遵循相同的路径......解决方案是在延迟(someSmallFractionOfTime * i)之后触发每个Layer / Animation,其中i递增。

所以我将动画部分提取为新的函数/方法/消息(无论它叫什么)

- (void) kickoffLaserAnimationWithPath: (NSArray *) path  {
CGPoint start = [(NSValue*)[path objectAtIndex:0] CGPointValue];
CALayer* laserLayer = [CALayer layer];
laserLayer.bounds = CGRectMake(start.x, start.y, 20, 10);
laserLayer.position = CGPointMake(start.x, start.y);
laserLayer.contents = (id)[UIImage imageNamed:@"Laser.png"].CGImage;
[self.layer addSublayer:laserLayer];

CAKeyframeAnimation* anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
anim.values = path;
anim.duration = ([path count] * laserSpeed);
anim.removedOnCompletion = NO;
anim.delegate = self;
anim.rotationMode = kCAAnimationRotateAuto;
[anim setValue:@"Fire" forKey:@"Action"];
[anim setValue:laserLayer forKey:@"Layer"];
[laserLayer addAnimation:anim forKey:nil];
isAnimating = YES;

}

并在循环中调用它,如下所示:

NSArray* path = [board caclulatePath:s];
    [CATransaction begin];
    [CATransaction setValue:[NSNumber numberWithFloat:([path count] * laserSpeed)] forKey:kCATransactionAnimationDuration];
    float numBetweenPoints = (float)((float)s.frame.size.height / (float)10) * 2;
    float delay = (laserSpeed / numBetweenPoints);
    for (int i = 0; i < [path count]; i++)
    {
        [self performSelector:@selector(kickoffLaserAnimationWithPath:) withObject:path afterDelay:(delay*i)];

    }
    [CATransaction commit];

...瞧