美文网首页RAC和RxSwift
RxSwift 倒计时, UIButton setTitle 闪

RxSwift 倒计时, UIButton setTitle 闪

作者: goodl | 来源:发表于2019-02-20 21:51 被阅读0次

按钮倒计时修设置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) // 在后

相关文章

网友评论

    本文标题:RxSwift 倒计时, UIButton setTitle 闪

    本文链接:https://www.haomeiwen.com/subject/bjvkyqtx.html