是否可以使用新的 #keyPath 关键字对 Swift 3 中的 CATransform3D 属性使用增强的键路径(如 here 所述)?

换句话说,要替换

let scaleAnimation = CABasicAnimation(keyPath: "transform.scale")

用类似的东西
let scaleAnimation = CABasicAnimation(keyPath:  #keyPath(CALayer.transform.???))

最佳答案

应该使用 CAValueFunction。

let scaleAnimation = CABasicAnimation(keyPath: "transform.scale")

->
let scaleAnimation = CABasicAnimation(keyPath: #keyPath(CALayer.transform))
scaleAnimation.valueFunction = CAValueFunction(name: kCAValueFunctionScale)


let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation")

->
let rotationAnimation = CABasicAnimation(keyPath: #keyPath(CALayer.transform))
rotationAnimation.valueFunction = CAValueFunction(name: kCAValueFunctionRotateZ)

等等。
  • rotation.x -> kCAValueFunctionRotateX
  • rotation.y -> kCAValueFunctionRotateY
  • rotation.z -> kCAValueFunctionRotateZ
  • rotation -> kCAValueFunctionRotateZ
  • scale.x -> kCAValueFunctionScaleX
  • scale.y -> kCAValueFunctionScaleY
  • scale.z -> kCAValueFunctionScaleZ
  • scale -> kCAValueFunctionScale
  • translation.x -> kCAValueFunctionTranslateX
  • translation.y -> kCAValueFunctionTranslateY
  • translation.z -> kCAValueFunctionTranslateZ
  • translation -> kCAValueFunctionTranslate
  • 关于swift3 - CATransform3D 关键路径与#keyPath,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41102217/

    10-12 19:27