ios - 使用核心动画后如何获取 View 的当前旋转?

标签 ios objective-c core-animation

我使用核心动画来旋转 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/

相关文章:

ios - "Format string is not string literal (potentially insecure)"

ios - 为 iOS 7.1 编程并使用 UIAlertView,这也适用于 iOS 8.0 吗?

ios - AFNetworking GET 请求 - 无法将responseObject转换为NSDictionary

ios - ALAssetsLibrary 从自定义相册中获取图像 ios

objective-c - 应用被拒绝。如何跟踪用户非续订订阅

ios - 在 AVPlayer 中播放视频

ios - 基础和 cocoa 框架之间的关系

objective-c - 如何加快 iOS 过渡和转场?

iphone - 是否可以使用 Core Animation 淡出 UIBarButtonItem?

ios - 动画 UIImageView 逐行出现在屏幕上