我有此代码完美的背景颜色:
UIView.animate(withDuration: 2, delay: 0.0, options:[UIViewAnimationOptions.repeat, UIViewAnimationOptions.autoreverse], animations: {
self.view.backgroundColor = UIColor(red: 0/255, green: 185/255, blue: 215/255, alpha: 1.0)
self.view.backgroundColor = UIColor(red: 0/255, green: 78/255, blue: 215/255, alpha: 1.0)
self.view.backgroundColor = UIColor(red: 0/255, green: 215/255, blue: 138/255, alpha: 1.0)
}, completion: nil)
,但后来我尝试使用它来对标签的背景颜色进行动画,但颜色较深,并且不起作用:
UIView.animate(withDuration: 2, delay: 0.0, options:[UIViewAnimationOptions.repeat, UIViewAnimationOptions.autoreverse], animations: {
self.topBar.backgroundColor = UIColor(red: 0/255, green: 159/255, blue: 184/255, alpha: 1.0)
self.topBar.backgroundColor = UIColor(red: 0/255, green: 67/255, blue: 184/255, alpha: 1.0)
self.topBar.backgroundColor = UIColor(red: 0/255, green: 184/255, blue: 117/255, alpha: 1.0)
}, completion: nil)
有什么问题,还是我该怎么做来对标签的背景颜色进行动画?
尝试Animate Layer.backgroundColor而不是背景彩色
此代码对我有用
UIView.animate(withDuration: 2, delay: 0.0, options:[UIViewAnimationOptions.repeat, UIViewAnimationOptions.autoreverse], animations: {
self.label.layer.backgroundColor = UIColor(red: 0/255, green: 185/255, blue: 215/255, alpha: 1.0).cgColor
self.label.layer.backgroundColor = UIColor(red: 0/255, green: 78/255, blue: 215/255, alpha: 1.0).cgColor
self.label.layer.backgroundColor = UIColor(red: 0/255, green: 215/255, blue: 138/255, alpha: 1.0).cgColor
}, completion: nil)