8

CABasicAnimation に問題があります。その投稿に似ています: CABasicAnimation 回転は元の位置に戻ります

だから、私はtouchMoveで回転するuiimageviewを持っています。「慣性のアニメーション」を行う touchEnd メソッドを呼び出します。

-(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 になります。現在の回転角度が必要です。実験として、私はメソッドを宣言します

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

そしてそれは働いているようです。しかし、animationDidStop で UIImageView の角度または CGAffineTransform の値を取得する方法がわかりません。することさえ可能ですか?ありがとう。

4

1 に答える 1

10

アニメーションの実行中にレイヤーのプロパティを取得するには、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;
    }
于 2012-04-28T13:48:16.050 に答える