我正在学习动画,我想使用CAKeyframeAnimation。在Apple Developer上,我找到了以下代码片段:
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但是我还没有找到关于如何将这个动画对象添加到视图中的解决方案。如何使它工作并应用于任何事情?
发布于 2017-10-09 15:26:38
将其添加到视图的layer和add(_: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)注意,“相对”启动时间和持续时间值是整个动画持续时间的百分比。
https://stackoverflow.com/questions/46636090
复制相似问题