打电话
setTitleColor()
动画块内部不起作用,因为
UIView.animate
仅适用于可设置动画的属性,如
infoButton.backgroundColor
.
不幸的是,按钮属性如
tintColor
aren't animatable
。你也不应该直接访问按钮的
titleLabel
,即
nil
用于系统按钮。
相反,您可以使用
UIView.transition
具有
setTitleColor()
。然后你可以把它放在
Timer
…我通常不喜欢在动画中使用计时器,但我想不出其他方法。
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let colors: [UIColor] = [.blue, .green, .yellow, .red, .purple]
var currentIndex = 0
_ = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { timer in
if colors.indices.contains(currentIndex) {
UIView.transition(with: self.infoButton, duration: 0.5, options: [.transitionCrossDissolve, .allowUserInteraction]) {
self.infoButton.setTitleColor(colors[currentIndex], for: .normal)
}
currentIndex += 1
} else {
currentIndex = 0
}
}
}
结果: