CABasicAnimation how to get current angle of rotation
Asked Answered
M

1

8

I have some problem with CABasicAnimation. It`s similar to that post: CABasicAnimation rotate returns to original position

So, i have uiimageview that rotate in touchMove. In touchEnd invoke method that do "animation of inertia" :

-(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 invoke touchBegan before animationRotation ended, angle of rotation is beginValue. I need cath current angle of rotation. As an experiment, i declare method

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

and it's seems working. but I don't know how get that value of angle or CGAffineTransform for my UIImageView in animationDidStop. It's even possible to do? Thanks.

Matted answered 8/4, 2012 at 10:44 Comment(0)
A
9

you should use presentationLayer method to get layer properties during animation in flight.

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;
    }
Autotrophic answered 28/4, 2012 at 13:48 Comment(2)
Thanks, i tried this, it works, but not quite as it should. I used NSTimer animation.. but thanks anyway :-)Matted
Upvoted for the method of getting the currently visible rotation of the layer.Yapon

© 2022 - 2024 — McMap. All rights reserved.