一、常用的RxSwift基本UI函数
1、通知
func testNotification() {
NotificationCenter.default.rx.notification(Notification.Name(rawValue: "TextNotification")).subscribe { noti in
print("接到通知!")
}.disposed(by: disposeBag)
}
override func touchesBegan(_ touches: Set, with event: UIEvent?) {
// 发送通知
NotificationCenter.default.post(name: Notification.Name(rawValue: "TextNotification"), object: nil)
}
2、手势
func testGestureRecognizers() {
label.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer()
label.addGestureRecognizer(tap)
tap.rx.event.subscribe(onNext: { tap in
print("\(tap.view)")
})
}
3、kvo观察
let model: FCTestModel = FCTestModel()
func testKVO() {
// skip(1)跳过第一次初始化的观察
self.model.rx.observeWeakly(String.self, "name").skip(1).subscribe(onNext: { value in
print(value)
}).disposed(by: disposeBag)
}
class FCTestModel: NSObject {
//必须是继承NSObject, 且属性是有@objc dynamic的,swift的kvo底层也是OC的runtime的那一套的
@objc dynamic var name: String?
}
4、scrollView
// scrollView滚动偏离的事件
func testScroll() {
scrollView.rx.contentOffset
.subscribe(onNext: { [weak self](content) in
print("\(content.x),\(content.y)")
})
.disposed(by: disposeBag)
}
5、textField
func testTextField() {
self.textField.rx.text.orEmpty.subscribe(onNext: { value in
self.label.text = value
print(value)
}).disposed(by: disposeBag)*/
self.textField.rx.text.orEmpty.bind(to: self.label.rx.text).disposed(by: disposeBag)
// 这两种方式都能实现一样的功能
}
6、button
func testBtn() {
// tap事件默认是touchupinside的
self.btn.rx.tap.subscribe(onNext: { btn in
print("点击了按钮")
}).disposed(by: disposeBag)
// 其他点击事件
self.btn.rx.controlEvent(.touchUpInside).subscribe(onNext: { btn in
print("点击了按钮")
}).disposed(by: disposeBag)
}
7、timer
func testTimer() {
// 第一个参数: 多少s后开始计时
// 第二个参数: 多少s计时一次
// 第三个参数: 哪个线程回调
Observable.timer(.seconds(10), period: .seconds(1), scheduler: MainScheduler.instance).subscribe(onNext: { num in
print(num)
}).disposed(by: disposeBag)
//1s后开始计时,0、1、2、3、4.....
Observable.interval(.seconds(1), scheduler: MainScheduler.instance).subscribe(onNext: { num in
print(num)
}).disposed(by: disposeBag)
// 倒计时
Observable.interval(.seconds(1), scheduler: MainScheduler.instance).map{60-$0}.subscribe(onNext: { num in
print(num)
}).disposed(by: disposeBag)
}
func testTimer() {
// 短信验证码的倒计时
// takeUntil为真的时候结束
Observable.interval(.seconds(1), scheduler: MainScheduler.instance).map{60-$0}.take(until: {$0<=0}).subscribe(onNext: { num in
print("倒计时:\(num)s")
},onCompleted: {
print("点击发送短信验证码")
}).disposed(by: disposeBag)
}
// 简单登录页面 账号、短信验证码、短信验证码的按钮、登录按钮的一些交互
func setupRx() {
let phoneOb = self.phoneTextField.rx.text.orEmpty.map({$0.count == 11})
let smsOb = self.smsTextField.rx.text.orEmpty.map({$0.count == 4})
let protocolBtnOb = self.protocolButton.rx.tap.scan(false) { current, _ in
print("protocolBtnOb.current-\(current)")
return !current
}.startWith(false)
_ = protocolBtnOb.bind(to: self.protocolButton.rx.isSelected)
_ = Observable.combineLatest(phoneOb, smsOb, protocolBtnOb, resultSelector: { b1, b2, b3 -> Bool in
print("\(b1),\(b2),\(b3)")
return b1 && b2 && b3
}).bind(to: self.loginButton.rx.isEnabled).disposed(by: disposeBag)
_ = loginButton.rx.tap.subscribe(onNext: {
print("点击了登录按钮")
})
let smsBtnOb = BehaviorSubject(value: true)
let smsNameOb = BehaviorSubject(value: "获取验证码")
smsNameOb.bind(to: smsButton.rx.title()).disposed(by: disposeBag)
_ = smsButton.rx.tap.subscribe(onNext: {
smsBtnOb.onNext(false)
Observable.interval(.seconds(1), scheduler: MainScheduler.instance).map({60 - $0}).take(while: {$0 > 0}).subscribe(onNext: { num in
smsNameOb.onNext("\(num)s")
},onCompleted: {
smsNameOb.onNext("获取验证码")
smsBtnOb.onNext(true)
})
}).disposed(by: disposeBag)
Observable.combineLatest(phoneOb, smsBtnOb){$0 && $1}.bind(to: self.smsButton.rx.isEnabled).disposed(by: disposeBag)
}
参考文章:RxSwift常用基础知识