RxSwift-初识

2020-01-03  本文已影响0人  lmfei

RxSwift简介

RxSwift API初体验

Button的使用
func learButton() {
    button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
    
    button.rx.tap
        .subscribe(onNext: { [weak self] in
            print("点击了button")
            self?.button.backgroundColor = UIColor.blue
        })
        .disposed(by: disposeBag)
}
TestField的使用
func learTextField() {
    textField.rx.text.orEmpty.changed.subscribe(onNext: { (text) in
        print(text)
    }).disposed(by: disposeBag)
    
    textField.rx.text.bind(to: button.rx.title()).disposed(by: disposeBag)
}
ScrollView的使用
func learnScroll() {
    scrollView.rx.contentOffset
        .subscribe(onNext: { (point) in
            print(point)
        })
        .disposed(by: disposeBag)
}
KVO的使用
func learKVO() {
    self.personal.rx.observeWeakly(String.self, "name")
        .subscribe(onNext: { (value) in
            print(value!)
        })
        .disposed(by: disposeBag)
}
Notification的使用
func learNotification() {
    NotificationCenter.default.rx.notification(UIResponder.keyboardWillShowNotification)
        .subscribe(onNext: { (event) in
            print(event)
        })
        .disposed(by: disposeBag)
}
GestureRecognizer的使用
func learGestureRecognizer() {
    let tap = UITapGestureRecognizer()
    lab.isUserInteractionEnabled = true
    lab.addGestureRecognizer(tap)
    tap.rx.event.subscribe {
        (event) in
        print("点了lab")
        }.disposed(by: disposeBag)
}

Timer的使用
func learTimer() {
    let timer:Observable<Int> = Observable<Int>.interval(1, scheduler: MainScheduler.instance)
    timer.subscribe(onNext: { (num) in
        print(num)
    }).disposed(by: disposeBag)
}

还有什么是一句代码解决不了的事,如果有,那就再加一行!!!

生活如此美好,今天就点到为止。。。

上一篇 下一篇

猜你喜欢

热点阅读