【问题标题】:Get UIImageView rotation value in Swift在 Swift 中获取 UIImageView 旋转值
【发布时间】:2014-12-25 06:49:54
【问题描述】:

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

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

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

【问题讨论】:

    标签: ios xcode swift rotation presentation-layer


    【解决方案1】:

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

    let zKeyPath = "layer.presentationLayer.transform.rotation.z"
    let imageRotation = (self.imageView.valueForKeyPath(zKeyPath) as? NSNumber)?.floatValue ?? 0.0
    

    如果键路径不是,最后的“nil-coalescing operator”?? 将值设置为0.0 设置(或者不是NSNumber),这模仿了Objective-C的行为,其中 将floatValue 消息发送到nil 也会返回0.0

    【讨论】:

    • 非常感谢,这正是我要找的!
    猜你喜欢
    • 2018-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多