Rx swift 获取验证码逻辑

 var disposable: Disposable? = nil


 self.disposable?.dispose()
        self.disposable = Observable.timer(0, period: 1, scheduler: MainScheduler.instance).map { $0 + 1 }
                .map { self.countDownSeconds - $0 }
                .do(onNext: {  [weak self] (element) in
                    guard let `self` = self else { return }
                    if element == 0 {
                        self.disposable?.dispose()
                        self.verifyGet.isEnabled = true
                        self.verifyGet.titleLabel?.text = "重新获取"
                        self.verifyGet.setTitleColor(AppBlue, for: .normal)
                        self.verifyGet.setTitle("重新获取", for: .normal)
                    } else {
                        self.verifyGet.isEnabled = false
                    }
                })
                .subscribe(onNext: { [weak self] (element) in
                    guard let `self` = self else { return }
                    self.verifyGet.titleLabel?.text = "\(element)秒后"
                    self.verifyGet.setTitleColor(UIColor.lightGray, for: .normal)
                    self.verifyGet.setTitle("\(element)秒后", for: .normal)
                })



你可能感兴趣的:(Rx swift 获取验证码逻辑)