で作成した回転アニメーションを使用していCABasicAnimation
ます。UIView
2秒以上回転します。UIView
しかし、私は触れられたときにそれを止めることができる必要があります。アニメーションを削除すると、ビューはアニメーションが開始される前と同じ位置になります。
これが私のアニメーションコードです:
float duration = 2.0;
float rotationAngle = rotationDirection * ang * speed * duration;
//rotationAngle = 3*(2*M_PI);//(double)rotationAngle % (double)(2*M_PI) ;
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: rotationAngle ];
rotationAnimation.duration = duration;
rotationAnimation.cumulative = YES;
rotationAnimation.removedOnCompletion = NO;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
rotationAnimation.fillMode = kCAFillModeForwards;
rotationAnimation.delegate = self;
[self.view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
UIView
触れたときに、その回転を正しい位置で停止するにはどうすればよいですか?タッチ部分の管理方法は知っていますが、アニメーションの現在の角度でビューを停止する方法がわかりません。
解決策: プレゼンテーション層の角度を取得し、アニメーションを削除し、ビューの変換を設定することで、問題を解決しました。コードは次のとおりです。
[self.view.layer removeAllAnimations];
CALayer* presentLayer = self.view.layer.presentationLayer;
float currentAngle = [(NSNumber *)[presentLayer valueForKeyPath:@"transform.rotation.z"] floatValue];
self.view.transform = CGAffineTransformMakeRotation(currentAngle);