以代码实践为主,在主要地方作了注释,方便日后查看和使用
- 声明DisposeBag的变量, 使用.disposed(by: bag)(
旧版是.addDisposableTo(bag)
),则RxSwift订阅对象会随控制器销毁
fileprivate lazy var bag: DisposeBag = DisposeBag()
事件源头 | 观察者 | 订阅者 |
---|---|---|
触发事件 | 观察到点击事件 | 处理点击事件 |
RxSwift实现函数响应式编程,与观察者模式很类似
- Subject
需要导入
import RxSwift
- PublishSubject、replayObject、BehaviorSubject、Variable
//MARK: PublishSubject 订阅者只能接收--->订阅之后发出的事件
let publishSub = PublishSubject<String>()
// 这个事件接收不到
publishSub.onNext("18")
publishSub.subscribe { (event: Event<String>) in
}.disposed(by: bag)
// 这个事件可以接收
publishSub.onNext("111")
//MARK: replayObject 订阅者可以接收订阅之前和订阅之后的事件
// .create(bufferSize: 2) --->指定buffer大小,决定接收事件个数
// .createUnbounded() ---> 无边界buffer,接收所有事件
let replaySub = ReplaySubject<String>.createUnbounded()
replaySub.onNext("a")
replaySub.onNext("b")
replaySub.onNext("c")
replaySub.subscribe { (event: Event<String>) in
}.disposed(by: bag)
replaySub.onNext("d")
//MARK: BehaviorSubject 订阅者可以接收i订阅之前最后一个事件
let behavior = BehaviorSubject(value: "hehavior")
behavior.onNext("a")
behavior.onNext("b")
//从这里开始接收
behavior.onNext("c")
behavior.subscribe { (event: Event<String>) in
}.disposed(by: bag)
behavior.onNext("f")
behavior.onNext("g")
//MARK:Variable
/*
*1.相当于behaviorSubject进行封箱
2.如果想将variable当成obserable,让订阅者订阅,需要 as obserable 转成 obserable
3.如果variable打算发出事件,直接修改对象value即可
*/
let variable = BehaviorRelay(value: "a")
variable.asObservable().subscribe { (event: Event<String>) in
print(event)
}.disposed(by: bag)
- 系统自带组件事件监听
需要导入
import RxSwift
import RxCocoa
//MARK:1.按钮点击
// 常规写法
test1Btn.addTarget(self, action: #selector(btn1Click), for: .touchUpInside)
test2Btn.addTarget(self, action: #selector(btn2Click), for: .touchUpInside)
// Rx写法
test1Btn.rx.tap.subscribe { (event: Event<()>) in
print("按钮1发生点击111")
}.disposed(by: bag)
test2Btn.rx.tap.subscribe { (event: Event<()>) in
print("按钮2发生点击222")
}.disposed(by: bag)
//MARK:监听UITextField文字改变
// 传统
textField1.delegate = self
textField2.delegate = self
// Rx
// 方式一:
textField1.rx.text.subscribe { (event :Event<String?>) in
print(event.element!!)
}.disposed(by: bag)
textField2.rx.text.subscribe { (event :Event<String?>) in
print(event.element!!)
}.disposed(by: bag)
// 方式二:
textField1.rx.text.subscribe(onNext: { (string :String?) in
print(string!)
}).disposed(by: bag)
textField2.rx.text.subscribe(onNext: { (string :String?) in
print(string!)
}).disposed(by: bag)
//MARK:将textField内容显示到label
textField1.rx.text.bind(to: lb1.rx.text).disposed(by: bag)
textField2.rx.text.bind(to: lb2.rx.text).disposed(by: bag)
//MARK:KVO
// 传统
lb1.addObserver(self, forKeyPath: "text", options: .new, context: nil)
// Rx
lb1.rx.observe(String.self, "text").subscribe(onNext: { (string: String?) in
print("observe")
}).disposed(by: bag)
//MARK:监听ScrollView的滚动
// 传统
scrollView.contentSize = CGSize(width: 10000, height: 0)
scrollView.delegate = self
// Rx
scrollView.rx.contentOffset.subscribe(onNext: {(point: CGPoint) in
}).disposed(by: bag)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
print(change!)
}
- 自定义事件never、just、create、range、repeat
// never
fileprivate func customRxObserable() {
let neverO = Observable<String>.never()
neverO.subscribe { (event: Event<String>) in
print(event)
}.disposed(by: bag)
// 创建empty
let empty = Observable<String>.empty()
empty.subscribe { (event: Event<String>) in
}.disposed(by: bag)
// just
let just = Observable.just("")
just.subscribe { (event: Event<String>) in
print(event)
}.disposed(by: bag)
// create
let create = Observable.create{(obserber: AnyObserver<Any>) -> Disposable in
obserber.onNext("111")
obserber.onNext("12")
obserber.onCompleted()
return Disposables.create()
}
create.subscribe { (event: Event<Any>) in
}.disposed(by: bag)
let myJust = Observable.create { (observer: AnyObserver<String>) -> Disposable in
observer.onNext("111")
observer.onCompleted()
return Disposables.create()
}
myJust.subscribe { (event: Event<String>) in
print(event)
}.disposed(by: bag)
// range
let range = Observable.range(start: 1, count: 10)
range.subscribe { (event: Event<Int>) in
print(event.element!)
}.disposed(by: bag)
// repeat
let repeatO = Observable.repeatElement("hello world")
repeatO.take(5).subscribe { (event: Event<String>) in
print(event)
}.disposed(by: bag)
/*
*.subscribe { (event: Event<String>) in
print(event)
}.disposed(by: bag)
*/
网友评论