rac5

Reactivecocoa 5.0

主要的类型

1.事件(Event)
2.监听器(Observer)
3.存根(Disposable)
4.信号(Signal)
主要用法

1.信号的创建

// MARK: - 0.创建信号的方法
    func createSignalMehods() {
        // 1.通过信号发生器创建(冷信号)
        let producer = SignalProducer.init { (observer, _) in
            print("新的订阅,启动操作")
            observer.send(value: "Hello")
            observer.send(value: "World")
        }

        let subscriber1 = Observer(value: { print("观察者1接收到值 \($0)") })
        let subscriber2 = Observer(value: { print("观察者2接收到值 \($0)") })

        print("观察者1订阅信号发生器")
        producer.start(subscriber1)
        print("观察者2订阅信号发生器")
        producer.start(subscriber2)
        //注意:发生器将再次启动工作

        // 2.通过管道创建(热信号)
        let (signalA, observerA) = Signal.pipe()
        let (signalB, observerB) = Signal.pipe()
        Signal.combineLatest(signalA, signalB).observeValues { (value) in
            print( "收到的值\(value.0) + \(value.1)")
        }
        observerA.send(value: "1")
        observerA.sendCompleted()
        observerB.send(value: "2")
        observerB.sendCompleted()
    }

2.信号合并

// MARK: - 4.信号联合
    func testCombineLatest() {

        // 通过管道创建
        let (signalA, observerA) = Signal.pipe()
        let (signalB, observerB) = Signal.pipe()
        Signal.combineLatest(signalA, signalB).observeValues { (value) in
            print( "收到的值\(value.0) + \(value.1)")
        }
        observerA.send(value: "1")
        observerA.sendCompleted()
        observerB.send(value: "2")
        observerB.sendCompleted()


    }
    // MARK: 信号联合
    func testZip() {
        let (signalA, observerA) = Signal.pipe()
        let (signalB, observerB) = Signal.pipe()

        Signal.zip(signalA, signalB).observeValues { (value) in
            print( "收到的值\(value.0) + \(value.1)")
        }

        signalA.zip(with: signalB).observeValues { (value) in

        }
        observerA.send(value: "1")
        observerA.sendCompleted()
        observerB.send(value: "2")
        observerB.sendCompleted()

    }

3.Scheduler调度器

// MARK: - 6.Scheduler(调度器)
    func testScheduler() {
        // 主线程上延时0.3秒调用
        QueueScheduler.main.schedule(after: Date.init(timeIntervalSinceNow: 0.3)) {
            print("主线程调用")
        }

        QueueScheduler.init().schedule(after: Date.init(timeIntervalSinceNow: 0.3)){
            print("子线程调用")
        }

    }

4.Delegate

let (signal, obser) = Signal.pipe()
    @IBAction func btnClick(btn: UIButton) {
        obser.send(value: "代理测试")
    }
    // MARK: - Delegate
    func testDelegate() {
        redView.signal.observeValues { (value) in
            print("按钮点击\(value)")
        }
    }

5.通知

// MARK: 通知
    func testNoti() {
     // 普通的通知方法
       NotificationCenter.default.reactive.notifications(forName: Notification.Name(rawValue: "home")).observeValues { (value) in
            print(value.object ?? "")
        }

        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "home"), object: nil)

        // 键盘的通知
        NotificationCenter.default.reactive.notifications(forName: Notification.Name(rawValue: "UIKeyboardWillShowNotification" ), object: nil).observeValues { (value) in
            print("键盘弹起")
        }
        NotificationCenter.default.reactive.notifications(forName: Notification.Name(rawValue: "UIKeyboardWillHideNotification"), object: nil).observeValues { (value) in
            print("键盘收起")
        }
    }

6.KVO

// MARK: - 9.KVO
    func testKVO() {
        let result = self.view.reactive.values(forKeyPath: "bounds")
        result.start { [weak self](rect) in
            print(self?.view ?? "")
            print(rect)
        }
    }

7.迭代器

// MARK: - 10.迭代器
    func testIterator() {

        // 数组的迭代器
        let array:[String] = ["name","name2"]
        var arrayIterator =  array.makeIterator()
        while let temp = arrayIterator.next() {
            print(temp)
        }

        // swift 系统自带的遍历
        array.forEach { (value) in
            print(value)
        }

        // 字典的迭代器
        let dict:[String: String] = ["key":"name", "key1":"name1"]
        var dictIterator =  dict.makeIterator()
        while let temp = dictIterator.next() {
            print(temp)
        }

        // swift 系统自带的遍历
        dict.forEach { (key, value) in
            print("\(key) + \(value)")
        }

    }

8、其他事件绑定

// UI绑定到Model  
       Signal.combineLatest(uerNameTF1.reactive.continuousTextValues,passwordTF1.reactive.continuousTextValues).observeValues { (name, password) in
            //print("name = \(name) + password = \(password)")
        }

        // 当输入框的两个值长度都大于或者等于6,按钮才可以点击
        Signal.combineLatest(uerNameTF1.reactive.continuousTextValues,passwordTF1.reactive.continuousTextValues).map { (name, password) -> Bool in
            return ((name?.characters.count)! >= 6 && (password?.characters.count)! >= 6)
        }.observeValues { [weak self](value) in
            print("合并\(value)")
            self?.loginBtn.isEnabled = value
        }

        // 参数省略        
        Signal.combineLatest(uerNameTF1.reactive.continuousTextValues,passwordTF1.reactive.continuousTextValues).map { $0?.characters.count ?? 0 >= 6 && $1?.characters.count ?? 0 >= 6
            }.observeValues { [weak self](value) in
                print("合并\(value)")
                self?.loginBtn.isEnabled = value
        }

        loginBtn.reactive.isEnabled <~ Signal.combineLatest(uerNameTF1.reactive.continuousTextValues,passwordTF1.reactive.continuousTextValues).map { $0?.characters.count ?? 0 >= 6 && $1?.characters.count ?? 0 >= 6
        }

摘自那一抹忧伤

你可能感兴趣的:(rac5)