我使用核心动画旋转视图,动画完成后我想知道视图的当前旋转,但是这给我带来了麻烦。

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];

在上面的代码中,我使用 [[[testView.layerpresentationLayer] valueForKey:@“transform.rotation.z”] floatValue] 方法来获取图层的当前旋转度,该方法在stackoverflow上被锁定,但对我不起作用谁知道为什么?

有人能帮我吗?

对不起,我错了。 [[[testView.layerpresentationLayer] valueForKeyPath:@“transform.rotation.z”] floatValue] 可以正常工作。

最佳答案

valueForKey:需要单个密钥。您需要使用valueForKeyPath:,因为您正在询问密钥路径的值(用句点分隔的几个密钥)。

关于ios - 使用核心动画后如何获取 View 的当前旋转?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20797374/

10-13 04:27