ios - 如何在 CAShapeLayer 动画中使用 Swift 4 KeyPath

标签 ios swift swift4

如果我编写 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/

相关文章:

ios - 不建议颜色文字 - Xcode 9

swift - 无法将类型 'NSImageInterpolation.Type' 的值转换为预期参数类型 '[NSImageRep.HintKey : Any]?'

Swift 可以放入 `if let` block 吗?

ios - 如何获取当前显示的 UIViewController 不在 AppDelegate 中?

ios - MoveItemAtIndexPath 中的 CollectionView reloadData 有时会导致重复图像

ios - 当应用程序处于未运行或后台状态时如何处理推送通知有效负载而不点击它

android - 如何开始Android/iOS配件开发?

ios - 如何快速读取文档目录中的 json 文件?

ios - 如何在我编辑该单元格时保持 UITableViewCell 处于选中状态?

ios - iOS8.4 和 Xcode 6.4 更新后,Xcode 应用安装失败