I have a custom UIView and I would like to animate its backgroundColor
property. This is an animatable property of a UIView
.
This is the code:
class ETTimerUIView: UIView {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
// other methods
func flashBg() {
UIView.animateWithDuration( 1.0, animations: {
self.backgroundColor = UIColor.colorYellow()
})
}
override func drawRect() {
// Something related to a timer I'm rendering
}
This code causes causes the animation to skip and the color to change immediately:
self.backgroundColor = UIColor.colorYellow() // Changes immediately to yellow
If I animate alpha, this animates from 1 to 0 over one second as expected:
self.alpha = 0 // animates
How do I animate a background color change in this situation?
- Implementing
drawRect
blocks backgroundColor animation, but no answer is provided yet. - Maybe this is why you can't combine
drawRect
andanimateWithDuration
, but I don't understand it much.
I guess I need to make a separate view--should this go in the storyboard in the same view controller? programmatically created?
Sorry, I'm really new to iOS and Swift.
completion
block fires immediately and switches it back (in the version that has a completion block). I think this is an animation issue. – SimplGyflashBg
when a timer completes, don't want to muddy the example with that, it's not important--could be called on app start, or when you tap the screen. It always never animates. – SimplGylet animation = CABasicAnimation(keyPath: "backgroundColor"); self.layer.addAnimation(animation, forKey: "flashBg")
. Also does not work. I can animateposition.x
. Wish I knew what was going on. I don't know how to get any useful debug output, even – SimplGy