RxSwift笔记 - RxCocoa 基础 (一)

    • UILabel
    • UITextField
    • UITextView
    • UIButton
    • UISlider
    • UIStepper
    • UISwitch
    • UISegmentedControl


UILabel

文字绑定到 UILabel

let label = UILabel()
// ...
/// 值绑定
Observable.just("Hello RxSwift")
    .bind(to: label.rx.text)
    .disposed(by: disposeBag)

UITextField

监听单个 textField 内容的变化

方式一:

let textField = UITextField()
// ...
textField.rx.text.orEmpty.asObservable().subscribe(onNext: {
    print("你输入的是: \($0)")
}).disposed(by: disposeBag)

方式二:

let textField = UITextField()
// ...
textField.rx.text.orEmpty.changed.subscribe(onNext: {
    print("你输入的是: \($0)")
}).disposed(by: disposeBag)

监听多个 textField 内容的变化

let inputField1 = UITextField()
let inputField2 = UITextField()
// ...
Observable.combineLatest(inputField1.rx.text.orEmpty, inputField2.rx.text.orEmpty) {
    (textValue1, textValue2) in
        print("你输入的号码是: \(textValue1)-\(textValue2)")
}

将内容绑定到其他控件上

Binder 方式实现

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)

Driver 方式实现

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)

事件监听

通过 rx.controlEvent 可以监听输入框的各种事件,且多个事件状态可以自由组合。除了各种 UI 控件都有的 touch 事件外,输入框还有如下几个独有的事件:

  • editingDidBegin:开始编辑(开始输入内容)
  • editingChanged:输入内容发生改变
  • editingDidEnd:结束编辑
  • editingDidEndOnExit:按下 return 键结束编辑
  • allEditingEvents:包含前面的所有编辑相关事件
let inputField1 = UITextField()
let inputField2 = UITextField()
// ...
inputField1.rx.controlEvent([.editingDidBegin]).asObservable().subscribe(onNext: {
    print("开始编辑")
}).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)

UITextView

UITextView 包含了 UITextField 的方法

UITextView 独有的方法

UITextView 还封装了如下几个委托回调方法:

  • didEndEditing:结束编辑
  • didChange:编辑内容发生改变
  • didChangeSelection:选中部分发生变化
let textView = UITextView()
// ...
textView.rx.didBeginEditing.subscribe(onNext: {
    print("开始编辑")
}).disposed(by: disposeBag)

textView.rx.didEndEditing.subscribe(onNext: {
    print("结束编辑")
}).disposed(by: disposeBag)

textView.rx.didChange.subscribe(onNext: {
    print("内容发生改变")
}).disposed(by: disposeBag)

textView.rx.didChangeSelection.subscribe(onNext: {
    print("选中部分发生改变")
}).disposed(by: disposeBag)

UIButton

按钮点击事件的绑定

let button = UIButton()
// ...
button.rx.tap.subscribe(onNext: {
    print("我被点击了")
}).disposed(by: disposeBag)

/// 另一种写法
button.rx.tap.bind {
    print("我也被点击了")
}.disposed(by: disposeBag)

按钮属性值的绑定

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)

UISlider

let slider = UISlider()
// ...
slider.rx.value.subscribe(onNext: {
    print("当前slider值为: \($0)")
}).disposed(by: disposeBag)

UIStepper

let stepper = UIStepper()
// ...
stepper.rx.value.subscribe(onNext: {
    print("当前stepper值为: \($0)")
}).disposed(by: disposeBag)

SliderStepper 绑定

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)

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: disposeBag)

/// 按钮和switch 之间的绑定
`switch`.rx.isOn.bind(to: button.rx.isEnabled).disposed(by: disposeBag)

UISegmentedControl

let segmentControl = UISegmentedControl()
// ...
segmentControl.rx.selectedSegmentIndex.subscribe(onNext: {
    print("当前页: \($0)")
}).disposed(by: disposeBag)

你可能感兴趣的:(iOS,Swift,RxSwift笔记)