1

I use core animation to rotate a view, after animation complete I want to kown the view's current rotation, but it trouble me.

CABasicAnimation *angleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
angleAnimation.toValue = @(45 * M_PI / 180);
angleAnimation.duration = 3;
angleAnimation.beginTime = CACurrentMediaTime();
angleAnimation.fillMode = kCAFillModeBoth;
angleAnimation.removedOnCompletion = NO;

[CATransaction begin];
[CATransaction setCompletionBlock:^{
    CGFloat rotationAngle = [[[testView.layer presentationLayer] valueForKey:@"transform.rotation.z"] floatValue];
    NSLog(@"rotationAngle = %f", rotationAngle);
}];
[testView.layer addAnimation:angleAnimation forKey:nil];
[CATransaction commit];

In above code,I use [[[testView.layer presentationLayer] valueForKey:@"transform.rotation.z"] floatValue] method to get the layer's current rotation , this method is seached on stackoverflow,but just not work for me.Anyone knows why?

Can someone help me?

Sorry for my mistake.[[[testView.layer presentationLayer] valueForKeyPath:@"transform.rotation.z"] floatValue] just work.

Vienta
  • 49
  • 5
  • 1
    Have you tried valueForKeyPath: ? – jrturton Dec 27 '13 at 08:40
  • May help. http://stackoverflow.com/questions/2051811/iphone-sdk-cgaffinetransform-getting-the-angle-of-rotation-of-an-object – KudoCC Dec 27 '13 at 08:44
  • @jrturton Yes!It work for me.Its my fault to use **valueFoKey** but not **valueForKeyPath** Thx! KudoCC It seems work for view's transform but not the view layer's transform.Thank you all the same! – Vienta Dec 27 '13 at 08:57
  • I've added that as an answer, then, rather than leave it in the comments. – jrturton Dec 27 '13 at 11:37

1 Answers1

3

valueForKey: expects a single key. You need to use valueForKeyPath:, since you are asking for the value of a key path (several keys separated by periods) instead.

jrturton
  • 118,105
  • 32
  • 252
  • 268