2012-04-08 23 views
7

CABasicAnimationに問題があります。そのポストに似ています:CABasicAnimation rotate returns to original positionCABasicAnimation現在の回転角度の取得方法

私はtouchMoveで回転するuiimageviewを持っています。このアニメーションは正常に動作しますが、animationRotationが終了する前に、私はtouchBeganを起動する場合は、回転角度があるBeginValueある

-(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; 
} 

:。touchEndでは、「慣性のアニメーション」を行うメソッドを呼び出します私は現在の回転角のカフを必要とします。実験として、私は方法を宣言します。

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

と思われます。私はどのように角度の値を取得するかわからないまたはCGAffineTransform私のUIImageViewのanimationDidStop。 することもできますか?ありがとう。

答えて

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のアニメーションを使用しましたが、とにかく感謝しています:-) – frankWhite

+0

レイヤーの現在表示されている回転を取得する方法のためにUpvoted。 – geraldWilliam

関連する問題