我使用核心动画来旋转 View ,动画完成后我想知道 View 的当前旋转,但这给我带来了麻烦。
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/