【发布时间】:2019-04-06 22:17:36
【问题描述】:
我正在尝试对图层的背景颜色从红色到蓝色进行简单的 CABasicAnimation。
添加动画并设置模型层的最终值后,动画闪烁为蓝色,然后再次变为红色,并动画为蓝色。
我的代码是:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// Create red layer and add to view
let redLayer = CALayer()
redLayer.backgroundColor = UIColor.red.cgColor
redLayer.position = view.center
redLayer.bounds.size = CGSize(width: 100, height: 100)
view.layer.addSublayer(redLayer)
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) {
// After a 1 second delay, animate from red to blue.
let anim = CABasicAnimation(keyPath: "backgroundColor")
anim.duration = 3
anim.fromValue = redLayer.backgroundColor
anim.toValue = UIColor.blue.cgColor
redLayer.add(anim, forKey: "")
// Set background color to final value
redLayer.backgroundColor = UIColor.blue.cgColor
}
}
这里发生了什么?
【问题讨论】:
标签: ios core-animation calayer cabasicanimation