本文demo下载
UISegmentedControl
segment.rx.selectedSegmentIndex.asObservable()
.subscribe(onNext:{
print("选择了第\($0)个")
})
.disposed(by: disposeBag)
UIActivityIndicatorView
通过UISwict控制UIActivityIndicatorView是否旋转,也可以控制左上角网络是否是否旋转。
switch1.rx.isOn
.bind(to: activityView.rx.isAnimating)
.disposed(by: disposeBag)
switch1.rx.isOn
.bind(to: UIApplication.shared.rx.isNetworkActivityIndicatorVisible)
.disposed(by: disposeBag)
UISlider
slider.rx.value.asObservable()
.subscribe(onNext:{
print("UISlider当前的值:\($0)")
})
.disposed(by: disposeBag)
UIStepper
steper.rx.value.asObservable()
.subscribe(onNext:{
print("UIStepper当前的值:\($0)")
})
.disposed(by: disposeBag)
通过修改UIStepper的值来改变slider的值
steper.rx.value.asObservable()
.map({Float($0)})
.bind(to: slider.rx.value)
.disposed(by: disposeBag)
UIDatePicker
datePicker.rx.date
.map{ [weak self] in
"选中的时间:" + self!.dateFormater.string(from: $0)
}
.bind(to:label.rx.text)
.disposed(by: disposeBag)
UIPickerView
导入RxDataSources使用起来更方便
单列的情况
private let adapter1 = RxPickerViewStringAdapter<[String]>(
components: [],
numberOfComponents: { _,_,_ in 1 },
numberOfRowsInComponent: { (_, _, items, _) -> Int in
return items.count},
titleForRow: { (_, _, items, row, _) -> String? in
return items[row]}
)
Observable.just(["1","2","3"])
.bind(to: pickerView.rx.items(adapter: adapter1))
.disposed(by: disposeBag
多列的情况
private let adapter2 = RxPickerViewStringAdapter<[[String]]>(
components: [],
numberOfComponents: { dataSource,pickerView,components in components.count },
numberOfRowsInComponent: { (_, _, components, component) -> Int in
return components[component].count},
titleForRow: { (_, _, components, row, component) -> String? in
return components[component][row]}
)
Observable.just([["1","2","3"],["A","B","C"]])
.bind(to: pickerView2.rx.items(adapter: adapter2))
.disposed(by: disposeBag)
手势
对UIGestureRecognizer增加了响应方法,手势使用方法都一致。
//滑动手势
swipeGesture.rx.event
.subscribe(onNext:{ [weak self] recognizer in
let point = recognizer.location(in: recognizer.view)
self?.showMessage(text: "上滑了",message: "x:\(point.x) y: \(point.y)")
})
.disposed(by: disposeBag)
//点击手势
tapGesture.rx.event
.subscribe(onNext:{ [weak self] recognizer in
let point = recognizer.location(in: recognizer.view)
self?.showMessage(text: "点击了",message: "x:\(point.x) y: \(point.y)")
})
.disposed(by: disposeBag)