如何在CAShapeLayer动画中使用Swift 4 KeyPath [英] How to use Swift 4 KeyPath in CAShapeLayer animation

查看:63
本文介绍了如何在CAShapeLayer动画中使用Swift 4 KeyPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我编写Swift 3代码,它将看起来像这样:

 让animation = CABasicAnimation(keyPath:#keyPath(CAShapeLayer.path)) 

但是我尝试对keyPath使用Swift 4新语法,但是我得到了:

 让keyPath = \ CAShapeLayer.pathlet animation = CABasicAnimation(keyPath:keyPath)//错误行 

>错误:无法将类型"ReferenceWritableKeyPath"的值转换为预期的参数类型字符串?"

在这种情况下,如何快速使用密钥路径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

> Error: Cannot convert value of type 'ReferenceWritableKeyPath' to expected argument type 'String?'

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆