Ios 以Swift格式获取UIImageView旋转值
我试图在Xcode 6.1中将Objective-C中的iOS项目重写为Swift,但我无法“翻译”Objective-C行: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返回 一个必
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
非常感谢,这就是我要找的!