首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用CAKeyframeAnimation?

如何使用CAKeyframeAnimation?
EN

Stack Overflow用户
提问于 2017-10-08 21:08:42
回答 1查看 2.2K关注 0票数 2

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

代码语言:javascript
复制
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

但是我还没有找到关于如何将这个动画对象添加到视图中的解决方案。如何使它工作并应用于任何事情?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-09 15:26:38

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

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

代码语言:javascript
复制
// 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)

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46636090

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档