我想改变UIVIew
动画的背景颜色和循环中的不同颜色。但颜色没有变化。这是我的代码:
UIView.animate(withDuration: 0.6, delay: 0.0, options:[.repeat, .autoreverse], animations: {
self.backgroundColor = UIColor(red: self.getRedComponant(), green: self.getGreenComponant(), blue: self.getBlueComponant(), alpha: CGFloat(1.0))
}, completion:nil)
请帮我找出问题所在。
对.repeat
不执行getColorComponent()
它重复每一次,而不是你可以添加一个计时器
这对我有用:
var timer = Timer()
override func viewDidLoad() {
super.viewDidLoad()
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { (timer) in
UIView.animate(withDuration: 0.6, delay: 0.0, animations: {
self.view.backgroundColor = UIColor(displayP3Red: self.getRedComponant(), green: self.getGreenComponant(), blue: self.getBlueComponant(), alpha: 1)
}, completion:nil)
})
}
func getRedComponant() -> CGFloat {
return CGFloat((arc4random() % 256)) / 255.0;
}
func getBlueComponant() -> CGFloat {
return CGFloat((arc4random() % 256)) / 255.0;
}
func getGreenComponant() -> CGFloat {
return CGFloat((arc4random() % 256)) / 255.0;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句