【问题标题】:How to use CAKeyframeAnimation?如何使用 CAKeyframeAnimation?
【发布时间】:2017-10-10 01:10:13
【问题描述】:

我正在学习动画,我想使用CAKeyframeAnimation。 在 Apple Developer 上我找到了以下代码 sn-p:

let colorKeyframeAnimation = CAKeyframeAnimation(keyPath: "backgroundColor")

colorKeyframeAnimation.values = [
    UIColor.red.cgColor,
    UIColor.green.cgColor,
    UIColor.blue.cgColor
]
colorKeyframeAnimation.keyTimes = [0, 0.5, 1]
colorKeyframeAnimation.duration = 2

但是我还没有找到关于如何添加这个动画对象来查看的解决方案。如何让它发挥作用并适用于任何事物?

【问题讨论】:

    标签: swift cakeyframeanimation


    【解决方案1】:

    您将其添加到视图的 layeradd(_:forKey:)


    或者,如果您不想使用 CoreAnimation 制作动画,可以使用 UIView.animateKeyframes API,例如:

    // if the animatedView isn't already .red, set it as such
    //
    // animatedView.backgroundColor = .red
    
    // then start your animation from the current color, to green, to blue
    
    UIView.animateKeyframes(withDuration: 2, delay: 0, options: [], animations: { 
        UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.5, animations: { 
            self.animatedView.backgroundColor = .green
        })
        UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.5, animations: { 
            self.animatedView.backgroundColor = .blue
        })
    }, completion: nil)
    

    请注意,“相对”开始时间和持续时间值是整个动画持续时间的百分比。

    【讨论】:

    • 我认为应该使用 CoreAnimation 而不是 CoreGraphics 来制作动画。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    • 1970-01-01
    相关资源
    最近更新 更多