Stop CABasicAnimation at a specific point

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

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);
+5
source share
2 answers

! Core Animation.

, Core Animation, , .

. , , . . , . , , . , .

.

. , , layer layer presentationLayer. :

CATransform3D myTransform = [(CALayer*)[self.view.layer presentationLayer] transform];

"" . , , , , , .

!

, , .

, ,

, . ? , , .

, CAAnimation. , :

CABasicAnimation rotate

+15

presentationLayer, . - .

:

self.view.layer.transform = [(CALayer*)[self.view.layer presentationLayer] transform];
[self.view.layer removeAnimationForKey:@"rotationAnimation"];
+5

All Articles