CABasicAnimation how to get the current rotation angle

I have a problem with CABasicAnimation. This is similar to this post: CABasicAnimation rotate returns to its original position

So, I have uiimageview that rotate in touchMove. The inertia animation method is called in the touchEnd method:

-(void)animationRotation: (float)beginValue { CABasicAnimation *anim; anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; anim.duration = 0.5; anim.repeatCount = 1; anim.fillMode = kCAFillModeForwards; anim.fromValue = [NSNumber numberWithFloat:beginValue]; [anim setDelegate:self]; anim.toValue = [NSNumber numberWithFloat:(360*M_PI/180 + beginValue)]; [appleView.layer addAnimation:anim forKey:@"transform"]; CGAffineTransform rot = CGAffineTransformMakeRotation(360*M_PI/180 + beginValue); appleView.transform = rot; } 

This animation works fine, but if I call touchBegan before the animation, then the rotation of the rotation angle starts from the beginning. I need a rotation angle. As an experiment, I declare a method

  -(vod) animationDidStop:(CAAnimation *)anim finished:(BOOL)flag { NSLog(@"Animation finished!"); } 

and it seems to work. but I don’t know how to get this angle value or CGAffineTransform for my UIImageView in animationDidStop. Can it even be done? Thanks.

+7
source share
1 answer

you must use the presentationLayer method to get the properties of the layer during in-flight animation.

so your code should be like this

  #define RADIANS_TO_DEGREES(__ANGLE__) ((__ANGLE__) / (float)M_PI * 180.0f) -(void)animationRotation: (float)beginValue { CABasicAnimation *anim; anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; anim.duration = 0.5; anim.repeatCount = 1; anim.fillMode = kCAFillModeForwards; anim.fromValue = [NSNumber numberWithFloat:beginValue]; [anim setDelegate:self]; //get current layer angle during animation in flight CALayer *currentLayer = (CALayer *)[appleView.layer presentationLayer]; float currentAngle = [(NSNumber *)[currentLayer valueForKeyPath:@"transform.rotation.z"] floatValue]; currentAngle = roundf(RADIANS_TO_DEGREES(currentAngle)); NSLog(@"current angle: %f",currentAngle); anim.toValue = [NSNumber numberWithFloat:(360*M_PI/180 + beginValue)]; [appleView.layer addAnimation:anim forKey:@"transform"]; CGAffineTransform rot = CGAffineTransformMakeRotation(360*M_PI/180 + beginValue); appleView.transform = rot; } 
+9
source

All Articles