Lau*_* B. 1 xcode rotation presentation-layer ios swift
我正在尝试将Objective-C中的iOS项目重写为Xcode 6.1中的Swift,但我不能"翻译"这个Objective-C行:
CGFloat imageRotation = [[self.imageView valueForKeyPath:@"layer.presentationLayer.transform.rotation.z"] floatValue];
Run Code Online (Sandbox Code Playgroud)
如何在Swift中获取我的UIImageView旋转值?
它在Swift中只是稍微复杂一些,因为valueForKeyPath返回一个可选项,必须将其解包然后显式转换为NSNumber.这可以(例如)使用可选链接和可选转换的组合来完成:
let zKeyPath = "layer.presentationLayer.transform.rotation.z"
let imageRotation = (self.imageView.valueForKeyPath(zKeyPath) as? NSNumber)?.floatValue ?? 0.0
Run Code Online (Sandbox Code Playgroud)
最后的"nil-coalescing operator" ??将值设置为0.0如果未设置(或不是NSNumber)关键路径,则模拟Objective-C的行为,其中发送floatValue消息也nil将返回0.0.
| 归档时间: |
|
| 查看次数: |
1405 次 |
| 最近记录: |