I'm attempting to do a simple CABasicAnimation of a layer's background color from red to blue.
After adding the animation and setting the model layer's final value, the animations flickers to blue before turning red again, and animating to blue.
My code is:
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
}
}
What's happening here?