框架学习1.0RxSwift简单熟悉

2019-07-24  本文已影响0人  数字d

1.cocopod导入 , DoveLarkRX是项目名称

  use_frameworks!

  # Pods for DoveLarkRX

target 'DoveLarkRX' do
    pod 'RxSwift', '~> 5'
    pod 'RxCocoa', '~> 5'

# RxTest and RxBlocking make the most sense in the context of unit/integration tests

#    pod 'RxBlocking', '~> 5'
#    pod 'RxTest', '~> 5'
end

2.网络请求代码实现


func setupNetWork(){
    
      
    let url = URL(string: "http://www.baidu.com")

    URLSession.shared.rx.response(request: URLRequest(url: url!))
        .subscribe(onNext:{response,data in
            print(response)
        })
        .disposed(by: DisposeBag())
 

    }

3.定时器实现


   var timer: Observable<Int>!

  func setupTimer(){
        
        timer = Observable<Int>.interval(1, scheduler: MainScheduler.instance)
        
        timer.subscribe(onNext:{(num) in
            print(num)
        })
            .disposed(by: disposeBag)
          
    }

4.键盘弹出

  func setupNotification(){
        
        NotificationCenter.default.rx.notification(UIResponder.keyboardWillShowNotification)
            .subscribe(onNext:{(noti) in
                print(noti)
            })
            .disposed(by: disposeBag)
    }

5.手势

    func setupGestureRecognise(){
        
        
        let tap = UITapGestureRecognizer()
        self.lab.addGestureRecognizer(tap)
        self.lab.isUserInteractionEnabled = true;
        tap.rx.event.subscribe(onNext:{(tap) in
            print(tap.view)
        })
            .disposed(by: disposeBag)
     
    }

6.scroView滑动检测

  func setupScrollview(){
        
        scview.rx.contentOffset.subscribe(onNext:{[weak self](content) in
            self?.view.backgroundColor = UIColor.init(red: content.y / 255 * 0.8, green: content.y / 255 * 0.6, blue: content.y / 255 * 0.3, alpha: 1)
        })
        .disposed(by: disposeBag)

        
    }

7.UITextField监听

    func setupTF(){
        
        self.tf.rx.text.orEmpty
            .subscribe(onNext:{(text) in
                print(text)
            })
            .disposed(by: disposeBag)
    }

8.按钮事件

    func setupBtn(){

        self.btn.isUserInteractionEnabled = true
        self.btn.rx.tap
            .subscribe(onNext:{() in
                print("点击事件")
//            self.person.name = "newName"
            })
            .disposed(by: disposeBag)
        
        self.btn.rx.controlEvent(.touchUpOutside)
        
    }

9.KVO

 func setupKVO() {
        
        self.person.addObserver(self, forKeyPath: "name", options: .new, context: nil)
        self.person.rx.observeWeakly(String.self, "name")
            .subscribe(onNext:{(value) in
                print(value as Any)
            })
        .disposed(by: disposeBag)
    }
    
    
        deinit {
            self.person.removeObserver(self, forKeyPath: "name", context: nil)
        }

代码地址:https://gitee.com/xgkp/dovelarkRx.git

上一篇下一篇

猜你喜欢

热点阅读