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.

Community
  • 1
  • 1
frankWhite
  • 1,523
  • 15
  • 21

1 Answers1

10

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;
    }
ytur
  • 1,232
  • 12
  • 22