上部にセグメント化されたコントロールを持つ単純なアプリを作成しました。コントロールの 2 つのセグメントのいずれかをクリックすると、UIImageView が回転し始めます。変換を CGAffineTransformIdentity に設定するためのリセット ボタンが接続されています。
この問題は、ビューの回転アニメーションを行うメソッドが、セグメントを前後に切り替えて 2 回目に呼び出されたときに発生します。リセットを押すと、最新のアニメーションのみが削除されます。アニメーションを完全にリセットして停止するには、もう一度セグメントを切り替える必要があります。
次のコードは、セグメントを選択して UIImageView を回転させると呼び出され、セグメント間をクリックすると明らかに 2 回目に呼び出されます。
// Begin the animation block and set its name
[UIView beginAnimations:@"Rotate Animation" context:nil];
// Set the duration of the animation in seconds (floating point value)
[UIView setAnimationDuration:0.5];
// Set the number of times the animation will repeat (NSIntegerMax setting would repeat indefinately) (floating point value)
[UIView setAnimationRepeatCount:NSIntegerMax];
// Set the animation to auto-reverse (complete the animation in one direction and then do it backwards)
[UIView setAnimationRepeatAutoreverses:YES];
// Animation curve dictates the speed over time of an animation (UIViewAnimationCurveEaseIn, UIViewAnimationCurveEaseOut, UIViewAnimationCurveEaseInOut, UIViewAnimationCurveLinear)
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
// Changes the imageViews transform property to rotate the view using CGAffineTransformRotate
// CGAffineTransformRotate(transformToStartFrom, angleToRotateInRadians)
// Starting transform property can be set to CGAffineTransformIdentity to start from views original transform state
// This can also be done using CGAffineTransformMakeRotation(angleInRadians) to start from the IdentityTransform without implicitly stating so
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, degreesToRadians(90));
[UIView commitAnimations];
リセットボタンはこのコードを呼び出します -
self.imageView.transform = CGAffineTransformIdentity;