对 CAAction 的 run 方法感到困惑

问题描述

我正在尝试了解此方法 run(forKey:object:arguments:) 如何在 Apple 的 documentation 的以下代码中完全运行:

let delegate = LayerDelegate()
     
lazy var sublayer: CALayer = {
    let layer = CALayer()
    layer.delegate = self.delegate
    
    return layer
}()
     
func moveSublayer() {
    guard let action = sublayer.action(forKey: "moveRight") else {
        return
    }
    
    action.run(forKey: "transform",object: sublayer,arguments: nil) // this line
}
     
class LayerDelegate: NSObject,CALayerDelegate {
    func action(for layer: CALayer,forKey event: String) -> CAAction? {
        
        guard event == "moveRight" else {
            return nil
        }
        
        let animation = CABasicAnimation()
        animation.valueFunction = CAValueFunction(name: CAValueFunctionName.translateX)
        animation.fromValue = 1
        animation.toValue = 300
        animation.duration = 2
        
        return animation
    }
}

我对 forKey 中的 action.run(forKey: "transform",arguments: nil) 参数特别困惑。

在文档中,它是这样说的:

动作的标识符。标识符可以是键或键路径 相对于 anObject、任意外部动作或 CALayer 中定义的动作标识符。

我知道渲染树中的动画列表就像一个字典,所以你用一个键查询列表,你会得到一个特定的动画作为值,这就是上面例子中发生的事情。子层使用“moveRight”查询动画列表:

guard let action = sublayer.action(forKey: "moveRight") else {
    return
}

然后使用以下方法返回操作:

func action(for layer: CALayer,forKey event: String) -> CAAction? {
    guard event == "moveRight" else {
        return nil
    }
    
    let animation = CABasicAnimation()
    animation.valueFunction = CAValueFunction(name: kCAValueFunctionTranslateX)
    animation.fromValue = 1
    animation.toValue = 300
    animation.duration = 2
    
    return animation
}

但是,forKey: "transform" 是干什么用的?我们已经使用“moveRight”键查询了动画列表并获得了值。为什么我们需要另一把钥匙?

此外,如果我要创建组合变换和不透明度或其他非变换属性的组动画怎么办?我必须使用什么来代替 forKey:“转换”?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)