按钮倒计时修设置UIButton的title时会发生闪烁的情况:
countDownDisposable = Observable<Int>.interval(1, scheduler: MainScheduler.instance)
.map { self.countDownSeconds - $0 }
.do(onNext: { [weak self] (second) in
if second == 0 {
self?.countDownDisposable?.dispose()
self?.button.isEnabled = true
self?.button.setTitle("发送验证码"), for: .normal)
}
})
.subscribe(onNext: { [weak self] (second) in
self?.button.setTitle("重新发送 \(second)", for: .normal)
})
解决方法 1:
设置UIButton为custom类型
let button = UIButton(type: UIButton.ButtonType.custom)
解决方法 2:
self.button.titleLabel?.text = "发送验证码" // 在前
self.button.setTitle("发送验证码", for: .normal) // 在后