-
-
Save gogobook/e312360c6f605037984a2ac043bec108 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 模擬 indicator 顯示/隱藏狀態 | |
let indicator = Variable(false) | |
// 按鈕 | |
lazy var loginButton: UIButton = { | |
let button = UIButton(type: .system) | |
button.tintColor = UIColor.red | |
button.setTitle("登入", for: .normal) | |
return button | |
}() | |
//... | |
// 依據是否正在讀取中來改變按鈕狀態 | |
self.indicator.asObservable().map{ !$0 }.bind(to: loginButton.rx.isEnabled).disposed(by: disposeBag) | |
// 按鈕點按 Observer | |
let t = self.loginButton.rx.tap | |
// Xcode 中跟上面連著寫會沒有提示...所以這邊特別用 t 來放 tap | |
t.flatMap { [weak self] () -> Observable<Void> in | |
print(1) | |
self?.indicator.value = true | |
// 5 秒後繼續 | |
return Observable<Void>.just().delay(5, scheduler: MainScheduler.instance) | |
}.subscribe(onNext: {[weak self] () in | |
print(2) | |
self?.indicator.value = false | |
}).disposed(by: disposeBag) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment