RxSwift使用小结

【本文适用于有一定swift基础的同学】

1、按钮点击

tapButton.rx.tap.subscribe({ [weak self] _ in
			guard let this = self else {
				return
			}
			guard let text = this.numberLabel.text else {
				return
			}
			guard let number = Int(text) else {
				return
			}
			this.numberLabel.text = String(number+1)
		}).addDisposableTo(disposeBag)

2、按钮长按

let longPressGesture = UILongPressGestureRecognizer()
		longPressGesture.rx.event
			.subscribe(onNext: { [weak self] _ in
				guard let this = self else {
					return
				}
				guard let text = this.numberLabel.text else {
					return
				}
				guard let number = Int(text) else {
					return
				}
				this.numberLabel.text = String(number+1)
			}).addDisposableTo(disposeBag)
		self.tapButton.addGestureRecognizer(longPressGesture)
		
		tapButton.rx.tap
			.subscribe(onNext: { [weak self] in
				guard let this = self else {
					return
				}
				guard let text = this.numberLabel.text else {
					return
				}
				guard let number = Int(text) else {
					return
				}
				this.numberLabel.text = String(number+1)
			}).addDisposableTo(disposeBag)
		
		resetButton.rx.tap
			.subscribe(onNext: { [weak self] in
				guard let this = self else {
					return
				}
				this.numberLabel.text = "0"
			}).addDisposableTo(disposeBag)

3、UISlider

slider.rx.value.subscribe({
			[weak self] _ in
			guard let this = self else {
				return
			}
			this.calculator()
		}).addDisposableTo(disposeBag)

4、UITextField

numberTextField.rx.controlEvent(.touchDown).subscribe({
			[weak self] _ in
			guard let this = self else {
				return
			}
			this.numberTextField.text = ""
		}).addDisposableTo(disposeBag)

5、Swipe

let gesture = UISwipeGestureRecognizer()
		gesture.direction = UISwipeGestureRecognizerDirection.down
		
		_ = gesture.rx.event.subscribe({
			[weak self] _ in
			guard let this = self else {
				return
			}
			this.view.endEditing(true)
		})
		
		view.addGestureRecognizer(gesture)

未完待续。。。

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s