00. RxSwift (0) ———— A Brief Experience

It will take about 3 minutes to finish reading this article.

This article will gives a brief experience of RxSwift. So, let’s begin to experience some basic uses of RxSwift Firstly.

1. Monitoring button clicks

1
2
3
4
5
6
7
8
9
10
11
button1.closeBtn.rx.controlEvent(.touchUpInside).subscribe { () in
print("123")
} onDisposed: {
print("456")
} .disposed(by: disposeBag)

button1.rx.tap.subscribe { () in
print("123")
} onDisposed: {
print("789")
} .disposed(by: disposeBag)

2. Monitoring UIScrollView scrolling

1
2
3
4
scrollView.contentSize = CGSize(width: 1000, height: 0)
scrollView.rx.contentOffset.subscribe(onNext: { (point : CGPoint) in
print(point)
}).addDisposableTo(bag)

3. Changing the text in a label

1
2
3
4
5
6
7
label1.rx.observe(String.self, "text").subscribe(onNext: { (str: String?) in    
print(str!)
}).addDisposableTo(bag)

label2.rx.observe(CGRect.self, "frame").subscribe(onNext: { (rect: CGRect?) in
print(rect!.width)
}).addDisposableTo(bag)

4. Monitoring the textFiled

1
2
3
textFiled.rx.text.changed.subscribe { (text) in
print("hello world")
}.disposed(by: disposeBag)

5. monitoring the notification

1
2
3
4
NotificationCenter.default.rx.notification(UIResponder.keyboardWillShowNotification)
.subscribe(onNext: { (notification) in
print("keypad ejection")
}).disposed(by: disposeBag)

6. Repeat operation

1
2
3
4
5
6
7
8
9
10
Observable.repeatElement("hello Swift")
.take(3)
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)

/*Execution results
hello Swift
hello Swift
hello Swift
*/

7. Operations of array

1
2
3
4
5
6
7
8
9
10
let obs = Observable.from(["1", "2", "3", "4"])
.subscribe(onNext: { print($0) })
obs.disposed(by: disposeBag)
/*Execution results
1
2
3
4
*/