Swift开发进阶

RxSwift 深入浅出(四)控件应用

2018-08-25  本文已影响88人  Cooci_和谐学习_不急不躁

RxSwift 这个框架看我就够了,这一篇我重点介绍RxSwift在实际开发控件中的运用。搞RxSwift,我是认真的

RxSwift 宝图镇博,咔咔点赞~~~~

Rxswift
import UIKit
import RxSwift
import RxCocoa

class ViewController: UIViewController {
    
    let disposeBG = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        testUITextField()
        testLabel()
    }
}

extension ViewController{
    
    func testUITextField(){
        //测试UITextField
        let textf = UITextField(frame: CGRect(x: 50, y: 100, width: 100, height: 30))
        textf.borderStyle = UITextBorderStyle.roundedRect
        self.view.addSubview(textf)
        
        let label = UILabel(frame: CGRect(x: 50, y: 150, width: 200, height: 30))
        label.textColor = UIColor.orange
        self.view.addSubview(label)
        
        let btn = UIButton(type: .system)
        btn.frame = CGRect(x: 50, y: 180, width: 100, height: 30)
        btn.setTitle("登录", for:UIControlState.normal)
        self.view.addSubview(btn)
        
       let inputOB = textf.rx.text.orEmpty.asDriver().throttle(0.5)
        // asDriver
        // 映射一下 ---> 给我们的label
        inputOB.map {"当前输入了:\($0.count)"}
            .drive(label.rx.text)
            .disposed(by: disposeBG)
        
        // 映射一下 (0/1) --> btn的响应能力
        inputOB.map { $0.count > 5 }
            .drive(btn.rx.isEnabled)
            .disposed(by: disposeBG)
        
        // 还原序列 --> btn的内容
        inputOB.asObservable()
            .bind(to: btn.rx.title())
            .disposed(by: disposeBG)
        
        // 不写了 ---> btn.rx.image / 需要修饰
        let text = Variable("Cooci")
        _ = textf.rx.textInput<->text
        
        btn.rx.tap
            .subscribe(onNext: { [weak self] in
                self?.view.backgroundColor = UIColor.orange
                print("点击了 \(text.value)")
                self?.navigationController?.pushViewController(UIStoryboard(name: "Main", bundle: nil)
                    .instantiateViewController(withIdentifier: "UIDemoVC"), animated: true)
            })
            .disposed(by: disposeBG)
        
    }
    
    
    
    func testLabel(){
        //测试label
        let label = UILabel(frame: CGRect(x: 10, y: 250, width: 100, height: 30))
        self.view.addSubview(label)
        
        let timer = Observable<Int>.interval(0.1, scheduler: MainScheduler.instance)
        timer.map {  String(format: "%0.2d:%0.2d", arguments: [($0/60%60),($0%60)])
        }
        .bind(to: label.rx.text)
        .disposed(by: disposeBG)
    }
    
}

上面是最基本的UILabelUIButton,UITextFiled的应用,下面看看其他控件


import UIKit
import RxSwift
import RxCocoa

class UIDemoVC: UIViewController {

    @IBOutlet weak var accountTF: UITextField!
    @IBOutlet weak var passwordTF: UITextField!
    @IBOutlet weak var statusLabel: UILabel!
    @IBOutlet weak var mySwitch: UISwitch!
    @IBOutlet weak var slider: UISlider!
    @IBOutlet weak var segmentedControl: UISegmentedControl!
    @IBOutlet weak var juhua: UIActivityIndicatorView!
    
    let disposeBG = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()

         // textFiled输入状态监听
        accountTF.rx.controlEvent(.editingDidBegin)
        .asObservable()
            .subscribe(onNext: { _ in
                print("开始输入账号")
            }).disposed(by: disposeBG)

        // 账号响应结束--->密码响应  /
        accountTF.rx.controlEvent(.editingDidEndOnExit)
            .asObservable()
            .subscribe(onNext: { [weak self](_) in
                self?.passwordTF.becomeFirstResponder()
            }).disposed(by: disposeBG)

        // 密码响应结束--->结束输入响应
        passwordTF.rx.controlEvent(.editingDidEndOnExit)
            .asObservable()
            .subscribe(onNext: { [weak self](_) in
                self?.passwordTF.resignFirstResponder()
            }).disposed(by: disposeBG)
        
        // 组合
        Observable.combineLatest(accountTF.rx.text.orEmpty, passwordTF.rx.text.orEmpty){ (account, password) -> String in
                return "你的账号: \(account) 密码: \(password)"
            }.map{$0}
            .bind(to: statusLabel.rx.text)
            .disposed(by: disposeBG)
        
        // 开关响应
        mySwitch.rx.isOn.asObservable()
            .subscribe(onNext: { (isOn) in
                print("当前开关的状态:\(isOn)")
            }).disposed(by: disposeBG)
        
        // segmentedControl 响应
        segmentedControl.rx.selectedSegmentIndex.asObservable()
            .subscribe(onNext: {print("当前选择了 \($0)")})
            .disposed(by: disposeBG)
        
        // 绑定菊花和开关
        mySwitch.rx.value.asObservable()
            .bind(to: juhua.rx.isAnimating)
            .disposed(by: disposeBG)
        
        // 滑杆的监控
        slider.rx.value.asObservable()
            .subscribe(onNext: { print("当前进度 \($0)")})
            .disposed(by: disposeBG)

    }

}
上一篇下一篇

猜你喜欢

热点阅读