ユーザー入力に基づいて UIButton を回転させるメソッドを実装したいと考えています。最初の入力時に、左に 45 度回転する必要があります。2 回目の入力では、最初の回転後に停止した位置からさらに 45 度回転する必要があります。
しかし、ボタンは 2 番目のアニメーションを開始する前の元の位置に戻ります。以下は私が使用する方法です。
- (void)spinLayer:(CALayer *)inLayer duration:(CFTimeInterval)inDuration
direction:(int)direction
{
CABasicAnimation* rotationAnimation;
// Rotate about the z axis
rotationAnimation =
[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
// Rotate 360 degress, in direction specified
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 1/2 * direction];
// Perform the rotation over this many seconds
rotationAnimation.duration = inDuration;
rotationAnimation.removedOnCompletion = NO;
rotationAnimation.fillMode = kCAFillModeForwards;
// Set the pacing of the animation
rotationAnimation.timingFunction =
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
// Add animation to the layer and make it so
[inLayer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}