2012-04-08 6 views
7

CABasicAnimation에 문제가 있습니다. 그 게시물과 비슷합니다 : CABasicAnimation rotate returns to original positionCABasicAnimation 현재 회전 각도를 얻는 방법

그래서, 나는 touchMove에서 회전 uiimageview 있습니다.

-(void)animationRotation: (float)beginValue 
{ 
    CABasicAnimation *anim; 
    anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; 
    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 
    anim.duration = 0.5; 
    anim.repeatCount = 1; 

    anim.fillMode = kCAFillModeForwards; 
    anim.fromValue = [NSNumber numberWithFloat:beginValue]; 
    [anim setDelegate:self];  

    anim.toValue = [NSNumber numberWithFloat:(360*M_PI/180 + beginValue)]; 
    [appleView.layer addAnimation:anim forKey:@"transform"]; 

    CGAffineTransform rot = CGAffineTransformMakeRotation(360*M_PI/180 + beginValue); 
    appleView.transform = rot; 
} 

이 애니메이션은 잘 작동하지만 animationRotation이 종료하기 전에 내가 touchBegan를 호출 할 경우, 회전 각도는 beginValue입니다 : touchEnd에서 "애니메이션 관성의"할 메소드를 호출합니다. 나는 칼날의 현재 회전 각을 필요로합니다. 실험으로, 나는 방법을 선언한다.

-(vod) animationDidStop:(CAAnimation *)anim finished:(BOOL)flag 
{ 
     NSLog(@"Animation finished!"); 
} 

그것은 작동하고있는 것처럼 보인다. 하지만 나는 angle의 가치를 얻는 방법이나 animationDidStop의 UIImageView를위한 CGAffineTransform을 얻는 방법을 모르겠습니다. 할 수도 있습니다. 감사.

답변

9

비행 중에 애니메이션을하는 동안 레이어 속성을 가져 오려면 presentationLayer 메서드를 사용해야합니다.

그래서 당신의 코드가

#define RADIANS_TO_DEGREES(__ANGLE__) ((__ANGLE__)/(float)M_PI * 180.0f) 

    -(void)animationRotation: (float)beginValue 
    { 
     CABasicAnimation *anim; 
     anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 
     anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 
     anim.duration = 0.5; 
     anim.repeatCount = 1; 

     anim.fillMode = kCAFillModeForwards; 
     anim.fromValue = [NSNumber numberWithFloat:beginValue]; 
     [anim setDelegate:self];  



    //get current layer angle during animation in flight 
     CALayer *currentLayer = (CALayer *)[appleView.layer presentationLayer];  
     float currentAngle = [(NSNumber *)[currentLayer valueForKeyPath:@"transform.rotation.z"] floatValue]; 
     currentAngle = roundf(RADIANS_TO_DEGREES(currentAngle));   

     NSLog(@"current angle: %f",currentAngle); 



     anim.toValue = [NSNumber numberWithFloat:(360*M_PI/180 + beginValue)]; 
     [appleView.layer addAnimation:anim forKey:@"transform"]; 

     CGAffineTransform rot = CGAffineTransformMakeRotation(360*M_PI/180 + beginValue); 
     appleView.transform = rot; 
    } 
+0

감사합니다, 다음과 같이해야한다, 나는 꽤 예상대로 그것은,이 작품을 시도했지만. 나는 NSTimer animation을 사용했다. 그러나 어쨌든 고맙다 :-) – frankWhite

+0

레이어의 현재 보이는 회전을 얻는 방법에 대해 Upvoted. – geraldWilliam

관련 문제