Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 以Swift格式获取UIImageView旋转值_Ios_Xcode_Swift_Rotation_Presentation Layer - Fatal编程技术网

Ios 以Swift格式获取UIImageView旋转值

Ios 以Swift格式获取UIImageView旋转值,ios,xcode,swift,rotation,presentation-layer,Ios,Xcode,Swift,Rotation,Presentation Layer,我试图在Xcode 6.1中将Objective-C中的iOS项目重写为Swift,但我无法“翻译”Objective-C行: CGFloat imageRotation = [[self.imageView valueForKeyPath:@"layer.presentationLayer.transform.rotation.z"] floatValue]; 如何在Swift中获取UIImageView旋转值?在Swift中它只是稍微复杂一点,因为valueForKeyPath返回 一个必

我试图在Xcode 6.1中将Objective-C中的iOS项目重写为Swift,但我无法“翻译”Objective-C行:

CGFloat imageRotation = [[self.imageView valueForKeyPath:@"layer.presentationLayer.transform.rotation.z"] floatValue];

如何在Swift中获取UIImageView旋转值?

在Swift中它只是稍微复杂一点,因为
valueForKeyPath
返回 一个必须展开然后显式转换为
NSNumber
的可选项。 这可以(例如)通过可选链接的组合来完成 和可选演员阵容:

let zKeyPath = "layer.presentationLayer.transform.rotation.z"
let imageRotation = (self.imageView.valueForKeyPath(zKeyPath) as? NSNumber)?.floatValue ?? 0.0
最后一个“无合并运算符”
??
将值设置为
0.0
,如果键路径不可用 设置(或不是
NSNumber
),这模仿Objective-C的行为,其中
floatValue
消息发送到
nil
也会返回
0.0

非常感谢,这就是我要找的!