Last active
July 13, 2017 09:09
-
-
Save mosluce/f739eef2c8eb59c66d1fff3061933aae 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
let disposeBag = DisposeBag() | |
// 模擬 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
@yoxisem544
補充一個 asDriver 會自動 share ,所以就不用再加 shareReplay(1)