问题描述
如果我编写Swift 3代码,它将看起来像这样:
让animation = CABasicAnimation(keyPath:#keyPath(CAShapeLayer.path))
但是我尝试对keyPath使用Swift 4新语法,但是我得到了:
让keyPath = \ CAShapeLayer.pathlet animation = CABasicAnimation(keyPath:keyPath)//错误行
在这种情况下,如何快速使用密钥路径4?
到目前为止, CABasicAnimation
仍使用旧的 String
keyPaths,因此您仍应使用#即使您使用的是Swift 4,keyPath(CAShapeLayer.path)
Apple将来可能会更新其所有API,以利用这些更安全的密钥路径引用.但是目前,您仍然受困于不安全的"字符串.
If I write Swift 3 code it would look like this:
let animation = CABasicAnimation(keyPath: #keyPath(CAShapeLayer.path))
But I tried to use Swift 4 new syntax for keyPath and I got:
let keyPath = \CAShapeLayer.path
let animation = CABasicAnimation(keyPath: keyPath) // error line
How can I use key path in this situation with swift 4?
As for now CABasicAnimation
still uses the old String
keyPaths so you should still use #keyPath(CAShapeLayer.path)
even though you are using Swift 4.
Apple will probably update all it's APIs in the future to make use of these safer key path references. But for now you are stuck with "unsafe" Strings.
这篇关于如何在CAShapeLayer动画中使用Swift 4 KeyPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!