美文网首页
RxSwift 的使用(一)

RxSwift 的使用(一)

作者: 洱舟 | 来源:发表于2021-03-09 16:06 被阅读0次

    基本用法

    let disposeBag = DisposeBag()
    
    /**UIButton*/
    
    //基本用法
    button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
    
    @objc func buttonTapped() {
        print("button Tapped")
    }
    
    //Rx用法
    
    button.rx.tap
        .subscribe(onNext: {
            print("button Tapped")
        })
        .disposed(by: disposeBag)
    
    
    /**代理*/
    
    //基本用法
    class ViewController: UIViewController {
        ...
        override func viewDidLoad() {
            super.viewDidLoad()
            scrollView.delegate = self
        }
    }
    
    extension ViewController: UIScrollViewDelegate {
        func scrollViewDidScroll(_ scrollView: UIScrollView) {
            print("contentOffset: \(scrollView.contentOffset)")
        }
    }
    
    //Rx用法
    
    class ViewController: UIViewController {
        ...
        override func viewDidLoad() {
            super.viewDidLoad()
    
            scrollView.rx.contentOffset
                .subscribe(onNext: { contentOffset in
                    print("contentOffset: \(contentOffset)")
                })
                .disposed(by: disposeBag)
        }
    }
    
    
    /**通知*/
    
    //基本用法
    var ntfObserver: NSObjectProtocol!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        ntfObserver = NotificationCenter.default.addObserver(
              forName: .UIApplicationWillEnterForeground,
              object: nil, queue: nil) { (notification) in
            print("Application Will Enter Foreground")
        }
    }
    
    deinit {
        NotificationCenter.default.removeObserver(ntfObserver)
    }
    
    
    //Rx用法
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        NotificationCenter.default.rx
            .notification(.UIApplicationWillEnterForeground)
            .subscribe(onNext: { (notification) in
                print("Application Will Enter Foreground")
            })
            .disposed(by: disposeBag)
    }
    
    
    
    /**手势*/
    
    //基本用法
    
    let tap = UITapGestureRecognizer.init(target: self, action: #selector(tapGestureRecognizerWithHandle))
    view.isUserInteractionEnabled = true
    view.addGestureRecognizer(tap)
    
    @objc func tapGestureRecognizerWithHandle() {
        print("Tap Tapped")
    }
    
    //Rx用法
     let tapBackground = UITapGestureRecognizer()
            tapBackground.rx.event
                .subscribe(onNext: { [weak self] _ in
                   print("Tap Tapped")
                })
                .disposed(by: disposeBag)
            view.addGestureRecognizer(tapBackground)
    
    /**KVO*/
    
    //Rx用法
    
    view
      .rx.observe(CGRect.self, "frame")
      .subscribe(onNext: { frame in
        ...
      })
      
    //or
    
    view
      .rx.observeWeakly(CGRect.self, "frame")
      .subscribe(onNext: { frame in
        ...
      })
    
    /**文本监听*/
    
    //Rx用法
    
    self.textFiled.rx.text.orEmpty
                .subscribe(onNext: { (text) in
                   print(text)
                })
                .disposed(by: disposeBag)
    
    
    /**闭包回调*/
    
    //基本用法
    URLSession.shared.dataTask(with: URLRequest(url: url)) {
        (data, response, error) in
        guard error == nil else {
            print("Data Task Error: \(error!)")
            return
        }
    
        guard let data = data else {
            print("Data Task Error: unknown")
            return
        }
    
        print("Data Task Success with count: \(data.count)")
    }.resume()
    
    
    //Rx用法
    URLSession.shared.rx.data(request: URLRequest(url: url))
        .subscribe(onNext: { data in
            print("Data Task Success with count: \(data.count)")
        }, onError: { error in
            print("Data Task Error: \(error)")
        })
        .disposed(by: disposeBag)
    
    

    相关文章

      网友评论

          本文标题:RxSwift 的使用(一)

          本文链接:https://www.haomeiwen.com/subject/zakbqltx.html