3

단일 레이어에 애니메이션을 적용하여 화면의 임의 경로를 따라 위치를 변경했습니다. 이제는이 애니메이션을 여러 번 복제하여 구석 구부러지는 환상을 보려고합니다. 코드를 CATransaction 안에 넣고 루프의 각 반복마다 시작 위치를 증가시키는 루프에 넣은 다음 루프 다음에 CATransaction을 커밋했습니다. 내가 볼 수있는 효과는 코드가 루프에 없다면 (즉, 단 하나의 레이어가 애니메이션으로 표시되는 경우) 애니메이션이 끝날 때 모든 레이어가 표시됩니다 (애니메이션 대리인이 animationDidStop에서 애니메이션을 끝내기 전에여러 CALayers를 동시에 성공적으로 애니메이트하려면 어떻게합니까?

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

[보드 caclulatePath : S]는 여기서 CGPoints를 나타내는 NSValues의있는 NSArray *를 반환 등)

내가 작성한 코드는 보인다.

동일한 경로를 따라 여러 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 할 것입니다 ... –

관련 문제