2012-11-08 3 views
2

내 아이폰 앱에서 다른 UIButton을 누르면 180도 회전 한 UIButton이 있습니다. 다시 UPCutton을 클릭하면 다시 180도 회전하여 다시 180도 회전합니다.CAKeyframeAnimation 마지막 지점에서 회전 반복

이 모든 것이 360도 프로세스가 처음 시작될 때마다 정상적으로 작동하지만 처음부터 다시 시작하려고 시도하면 180도 스냅되어 그 지점에서 회전하려고 시도합니다. 누구든지 올바른 방향으로 나를 가리킬 수 있습니까? 여기에 지금까지 내 코드 ... 그것을 사용할되도록 그때 레이어의 회전에 rotateMe.transform 회전을 업데이트 애니메이션의 완료에

showAnimation= [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"]; 
        showAnimation.duration = self.showAnimationDuration; 
        showAnimation.repeatCount = 1; 
        showAnimation.fillMode = kCAFillModeForwards; 
        showAnimation.removedOnCompletion = NO; 
        showAnimation.cumulative = YES; 
        showAnimation.delegate = self; 

float currentAngle =[[[rotateMe.layer presentationLayer] valueForKeyPath:@"transform.rotation.z"] floatValue]; 

//Rotate 180 degrees from current rotation 
showAnimation.values = [NSArray arrayWithObjects:  
         [NSNumber numberWithFloat:currentAngle], 
         [NSNumber numberWithFloat:currentAngle + (0.5 * M_PI)], 
         [NSNumber numberWithFloat:currentAngle + M_PI], nil]; 

[rotateMe.layer addAnimation:showAnimation forKey:@"show"]; 

입니다.

- (void)animationDidStop:(CAKeyframeAnimation *)anim finished:(BOOL)flag 
{ 
    float currentAngle =[[[self.layer presentationLayer] valueForKeyPath:@"transform.rotation.z"] floatValue]; 

    NSLog(@"End: %f", currentAngle); 
    rotateMe.transform = CGAffineTransformMakeRotation(0); 
} 

나는

[UIView animateWithDuration:1.0f] 
     animations:^{ 
      CGAffineTransform transform = CGAffineTransformRotate(rotateMe.transform, DEGREES_TO_RADIANS(179.999f)); 
      rotateMe.transform = transform; 
     } 
    ]; 

와 완벽하게 작업 효과를 달성하지만, 따라서 CAKeyframeAnimation, 애니메이션이 더 복잡하고 싶습니다.

답변

5

모든 키 프레임이 필요한 경우 애니메이션을 additive으로 구성하고 0에서 180도까지 애니메이션을 적용 할 수 있습니다. 다른 키 프레임이 필요하지 않은 경우 기본 애니메이션 및 byValue 속성을 사용하여 간단히 수행 할 수 있습니다. 다음에 애니메이션을 추가하면보기에있는 회전보다 180도 회전합니다.

대리자 콜백에서 실제 값을 설정하는 경우 채우기 모드가 필요하지 않으며 완료시 애니메이션을 제거하지 않아도됩니다.

키 프레임 애니메이션을 사용합니다 (위에서 설명한대로 : 0에서 180도까지 추가 및 애니메이션으로 만듭니다).

CAKeyframeAnimation *showAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"]; 
showAnimation.additive = YES; // Make the values relative to the current value 
showAnimation.values = @[0, /*all your intermediate values here... ,*/ M_PI]; 
showAnimation.duration = self.showAnimationDuration; 
showAnimation.delegate = self; 

[rotateMe.layer addAnimation:showAnimation forKey:@"show"]; 
+0

고마워요! 교장의 문제로 키 프레임 버전으로 잘못하고있는 것을 찾고 싶지만 올바르게 작동합니다. 위의 내용을 키 프레임 버전으로 작성하는 방법에 대한 예가 있습니까? – Jaaaaaay

+0

"애니메이션을 추가하고 애니메이션을 0도에서 180도까지 애니메이션으로 구성하십시오."라고 설명했던대로 할 수 있습니다. 내 대답에 키 프레임 애니메이션 코드를 추가했습니다. –

+0

감사합니다. 나는 답을 표시 할 것이다. – Jaaaaaay

관련 문제