I am using rotation animation created with CABasicAnimation. He spins a for UIViewmore than 2 seconds. But I need to stop it when it touches UIView. If I delete the animation, the view will be in the same position as before the animation started.
Here is my animation code:
float duration = 2.0;
float rotationAngle = rotationDirection * ang * speed * duration;
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"];
How can I stop the turn UIView, where is it when it is touched? I know how to control the touch part, but I can’t figure out how to stop viewing at the current angle of animation.
Solution:
I solved the problem by getting the presentation layer angle, deleting the animation, and setting the view transform. Here is the code:
[self.view.layer removeAllAnimations];
CALayer* presentLayer = self.view.layer.presentationLayer;
float currentAngle = [(NSNumber *)[presentLayer valueForKeyPath:@"transform.rotation.z"] floatValue];
self.view.transform = CGAffineTransformMakeRotation(currentAngle);
source
share