2

私のiPhoneアプリには、別のUIButtonが押されたときに180度回転して表示されるUIButtonがあり、もう一度クリックすると、ボタンはさらに180度回転して最初の場所に戻ります。

これは、完全な 360 度プロセスが最初に発生したときにすべて正常に機能しますが、最初からやり直そうとすると、180 度スナップし、その時点から回転しようとします。誰かが私を正しい方向に向けることができますか? これまでの私のコードは次のとおりです...

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

アニメーションが完了したら、rotateMe.transform の回転をレイヤーの回転に更新して、使用できるようにします。

- (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.

4

1 に答える 1

8

additiveすべてのキーフレームが必要な場合は、アニメーションを0度から180度の範囲でアニメーション化するように構成できます。別のキーフレームが必要ない場合は、基本的なアニメーションとbyValueプロパティを使用して簡単に行うことができます。次にアニメーションが追加されると、ビュー上での回転よりも180度多く回転します。

デリゲートコールバックで実際の値を設定している場合は、塗りつぶしモードは必要なく、完了時にアニメーションを削除する必要はありません。

CABasicAnimation *showAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
showAnimation.byValue = M_PI;
showAnimation.duration = self.showAnimationDuration;
showAnimation.delegate = self;

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

または、キーフレームアニメーションを使用します(上記で説明したように、追加して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"];
于 2012-11-08T22:21:16.007 に答える