3

私は、画面上の任意のパスに沿ってその位置を変更するために、1つのレイヤをアニメーション化することに成功しています。私は今、このアニメーションを何度も複製して、曲がりくねった曲がり角を錯覚させようとしています。コードをCATransaction内に置き、ループの各繰り返しの開始位置をインクリメントするループに入れ、ループの後にCATransactionをコミットします。アニメーションの最後に、ループ内にコードがない場合(つまり、アニメーションが1つだけの場合)、アニメーションの最後にすべてのレイヤが表示されます(アニメーションの終わりにデリゲートがアニメーションを終了する前に)複数のCALayersを同時にアニメーション化するにはどうすればよいですか?

Iがどのように見えるを書かれているコード:CGPointsを表すNSValuesのにNSArray *を返す:[SボードcaclulatePath]は

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

どのように私は後に(同じパスの後にlaser.pngの複数のコピーがありますか)効果を得ることができますか? [Laser.pngは20px x 20pxの赤い四角形です。

答えて

2

実際の問題は、各層が同じ経路を同時にたどっていたことです...解決策は、(someSmallFractionOfTime * 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]; 

出来上がり...

2

CAAnimationGroupをご覧ください。私はそれがあなたの問題に合うかもしれないと思います。

+1

私の質問には役に立たなかったが、これを見る価値があるものとして+1する –

関連する問題