如果我编写 Swift 3 代码,它将如下所示:
let animation = CABasicAnimation(keyPath: #keyPath(CAShapeLayer.path))
但我尝试对 keyPath 使用 Swift 4 新语法,我得到了:
let keyPath = \CAShapeLayer.path
let animation = CABasicAnimation(keyPath: keyPath) // error line
> Error: Cannot convert value of type 'ReferenceWritableKeyPath' to expected argument type 'String?'
在这种情况下如何使用 swift 4 的关键路径?
最佳答案
至于现在 CABasicAnimation
仍然使用旧的 String
keyPaths 所以你仍然应该使用 #keyPath(CAShapeLayer.path)
即使你是使用 Swift 4。
Apple 将来可能会更新其所有 API,以使用这些更安全的 key 路径引用。但是现在您只能使用“不安全”的字符串。
关于ios - 如何在 CAShapeLayer 动画中使用 Swift 4 KeyPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45564192/