Search code examples
iosobjective-ccore-animation

How can I get the view's current rotation after use core animation?


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.


Solution

  • 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.