UILabel

Text bound to UILabel

let label = UILabel() // ... Prompt (to: label.rx.text).disposed(by: disposeBag) /// The value binding Observable.just("Hello RxSwift").bind(to: label.rx.text).Copy the code

UITextField

Listen for changes in the contents of a single textField

A:

let textField = UITextField() // ... TextField. Rx. Text. OrEmpty. AsObservable (). The subscribe (onNext: {print (" you type: \ ($0) ")}), disposed (by: disposeBag)Copy the code

Method 2:

let textField = UITextField() // ... TextField. Rx. Text. OrEmpty. Changed. The subscribe (onNext: {print (" you type: \ ($0) ")}), disposed (by: disposeBag)Copy the code

Listen for changes in the contents of multiple TextFields

let inputField1 = UITextField() let inputField2 = UITextField() // ... Observable.combineLatest(inputField1.rx.text.orEmpty, inputField2.rx.text.orEmpty) { (textValue1, \(textValue1)-\(textValue2)")}Copy the code

Bind content to other controls

Binder implementation

let inputField  = UITextField()
let outputField = UITextField()
let label = UILabel()
let button = UIButton()
// ...
// UITextField --> UITextField
inputField.rx.text
    .bind(to: outputField.rx.text)
    .disposed(by: disposeBag)

// UITextField --> UILabel
inputField.rx.text
    .bind(to: label.rx.text)
    .disposed(by: disposeBag)

// UITextField --> UIButton
inputField.rx.text
    .map({ $0.count > 5 })
    .bind(to: button.rx.isEnabled)
    .disposed(by: disposeBag)
Copy the code

Driver implementation

let inputField  = UITextField()
let outputField = UITextField()
let label = UILabel()
let button = UIButton()
// ...
// UITextField --> UITextField
inputField.rx.text.orEmpty.asDriver()
    .drive(outputField.rx.text)
    .disposed(by: disposeBag)

// UITextField --> UILabel
inputField.rx.text
    .drive(label.rx.text)
    .disposed(by: disposeBag)

// UITextField --> UIButton
inputField.rx.text.orEmpty
    .asDriver()
    .map({ $0.count > 5 })
    .drive(button.rx.isEnabled)
    .disposed(by: disposeBag)
Copy the code

The rx. ControlEvent allows you to listen for events in the input box. Multiple event states can be combined freely. In addition to the touch events common to all UI controls, the input box has the following unique events:

  • EditingDidBegin: Start editing (start typing content)
  • EditingChanged: The input has changed
  • EditingDidEnd: End editing
  • EditingDidEndOnExit: Press the Return key to end editing
  • AllEditingEvents: Contains all previous edit-related events
let inputField1 = UITextField() let inputField2 = UITextField() // ... InputField1. Rx. ControlEvent ([. EditingDidBegin]) asObservable (). The subscribe (onNext: {print (" start editing ")}), disposed (by: disposeBag) inputField1.rx.controlEvent([.editingDidEndOnExit]).subscribe(onNext: { inputField2.becomeFirstResponder() }).disposed(by: disposeBag) inputField2.rx.controlEvent([.editingDidEndOnExit]).subscribe(onNext: { inputField2.resignFirstResponder() }).disposed(by: disposeBag)Copy the code

UITextView

UITextView contains methods of UITextField

UITextView unique method

UITextView also encapsulates the following delegate callback methods:

  • DidEndEditing: End editing
  • DidChange: Edit content changes
  • DidChangeSelection: The selected part changes
let textView = UITextView() // ... TextView. Rx. DidBeginEditing. Subscribe (onNext: {print (" start editing ")}), disposed (by: DisposeBag) textView. Rx. DidEndEditing. Subscribe (onNext: {print (" end edit ")}), disposed (by: DisposeBag) textView. Rx. DidChange. Subscribe (onNext: {print (" content is changed ")}), disposed (by: DisposeBag) textView. Rx. DidChangeSelection. Subscribe (onNext: {print (" selected change ")}), disposed (by: disposeBag)Copy the code

UIButton

Binding of button click events

let button = UIButton() // ... Butt.rx.tap. Subscribe (onNext: {print(" I'm clicked ")}). Prompt (by: Prompt (button. Rx.tap. Bind {print(" I am also clicked ")}. Disposed (by: disposeBag)Copy the code

Binding of button property values

let button = UIButton()
// ...
Observable.just("Click Me")
    .bind(to: button.rx.title(for: .normal))
    .disposed(by: disposeBag)

Observable.just(UIImage(named: "imageName"))
    .bind(to: button.rx.image())
    .disposed(by: disposeBag)

let switch1 = UISwitch()
switch1.rx.isOn.bind(to: button.rx.isEnabled)
    .disposed(by: disposeBag)
Copy the code

UISlider

let slider = UISlider() // ... The slider. The rx. Value. The subscribe (onNext: {print (" the current value of the slider is: \ ($0) ")}), disposed (by: disposeBag)Copy the code

UIStepper

let stepper = UIStepper() // ... Stepper. Rx. Value. The subscribe (onNext: {print (" current stepper value is: \ ($0) ")}), disposed (by: disposeBag)Copy the code

Slider 与 StepperThe binding

let slider = UISlider()
let stepper = UIStepper()
// ...
// slider -> stepper
slider.rx.value.map{ Double($0) }
    .bind(to: stepper.rx.value)
    .disposed(by: disposeBag)

// stepper -> slider
stepper.rx.value.map{ Float($0) }
    .bind(to: slider.rx.value)
    .disposed(by: disposeBag)
Copy the code

UISwitch

let `switch` = UISwitch() let button = UIButton() let activityView = UIActivityIndicatorView() // ... `switch`.rx.value .bind(to: activityView.rx.isAnimating) .disposed(by: disposeBag) `switch`.rx.isOn .bind(to: UIApplication.shared.rx.isNetworkActivityIndicatorVisible) .disposed(by: Prompt (by: disposeBag) /// Binding between button and switch 'switch'.rx.ison.bind (to: button.rx.isenabled). Disposed (by: disposeBag)Copy the code

UISegmentedControl

let segmentControl = UISegmentedControl() // ... SegmentControl. Rx. SelectedSegmentIndex. Subscribe (onNext: {print (" the current page: \ ($0) ")}), disposed (by: disposeBag)Copy the code

UIGestureRecognizer

RxCocoa + UIGestureRecognizer

let tapGesture = UITapGestureRecognizer()
view.addGestureRecognizer(tapGesture)
Copy the code

Tap

/ / subscribe to achieve tapGesture. Rx. Event. The subscribe (onNext: {_ in print (" tapped ")}), disposed (by: Prompt {_ in print("tapped")}. Disposed (by: disposeBag)Copy the code

Swipe

/ / add a slide gestures to let swipe = UISwipeGestureRecognizer () swipe. Direction =. Up the self. The addGestureRecognizer (swipe) / / Recognizer (onNext: {[weak self] recognizer in let point = recognizer.location(in: self? View) print (" upward sliding \ (point. X) - \ point. (y) ")}) disposed (by: Swipe. Direction =.down swipe. Rx.event. Bind {[weak self] recognizer in let point = recognizer.location(in: self? .view) print(" Swipe down \(point.x) - \(point.y)")}. Disposed (by: disposeBagCopy the code

RxGesture + UIGestureRecognizer

tapView = UIView()
// ...
view.addSubview(tapView)
Copy the code

Tap

// tap tapView.rx.tapGesture().when(.recognized) .subscribe(onNext: { _ in print("Tapped!!!" ) }).disposed(by: disposeBag) // double tap tapView.rx.tapGesture(numberOfTapsRequired: 2).when(.recognized) .subscribe(onNext: { _ in print("double tapped!!!" ) }).disposed(by: disposeBag)Copy the code

Swipe

// swipe down
tapView.rx.swipeGesture(.down).when(.recognized)
    .subscribe(onNext: { _ in
    print("Swipe down")
}).disposed(by: disposeBag)

// swipe horizontal
tapView.rx.swipeGesture([.left, .right])
    .subscribe(onNext: { _ in
    print("Swipe horizontal")
}).disposed(by: disposeBag)
Copy the code

Long press

// long press
tapView.rx.longPressGesture().when(.began)
    .subscribe(onNext: { _ in
    print("long press")
}).disposed(by: disposeBag)
Copy the code

Pan

// pan gesture
let panGesture = tapView.rx.panGesture().share(replay: 1)

panGesture.when(.changed).asTranslation()
    .subscribe(onNext: { [unowned self] translation, _ in
    self.tapView.transform = CGAffineTransform(translationX: translation.x, y: translation.y)
}).disposed(by: disposeBag)

panGesture.when(.ended).subscribe(onNext: { _ in
    print("pan gesture end")
}).disposed(by: disposeBag)
Copy the code

Rotation Gesture

// rotation gesture
let rotationGesture = tapView.rx.rotationGesture().share(replay: 1)

rotationGesture.when(.changed).asRotation()
    .subscribe(onNext: { [unowned self] rotation, _ in
    self.tapView.transform = CGAffineTransform(rotationAngle: rotation)
}).disposed(by: disposeBag)

rotationGesture.when(.ended)
    .subscribe(onNext: { _ in
    print("rotation gesture end")
}).disposed(by: disposeBag)
Copy the code

Pinch

// Pinch let pinchGesture = tapview.rx.pinchgesture ().share(replay: 1) pinchGesture.when(.changed) .asScale() .subscribe(onNext: { [unowned self] scale, _ in self.tapView.transform = CGAffineTransform(scaleX: scale, y: scale) }).disposed(by: disposeBag) pinchGesture.when(.ended) .subscribe(onNext: { _ in print("pinch end") }).disposed(by: disposeBag)Copy the code

Edge Pan

/ / edge translational EdgePan view. The rx. ScreenEdgePanGesture (edges: right.) the when. (recognized). The subscribe (onNext: { _ in print("rigjt edge") }).disposed(by: disposeBag) tapView.rx.anyGesture(.tap(), .swipe([.up, .right])) .when(.recognized) .subscribe(onNext: { _ in print("tap or up down") }).disposed(by: disposeBag)Copy the code

Since the | address

Swift Books download:Download address