ReactiveCocoa 5.0 swift版本

轉換的類型有:

RACSiganl 變為 Signal

  • 信號的第一種創建方式
    <pre><code> let producer = SignalProducer<Any, NoError>.init { (observe, disposable) in
    observe.send(value: "hello")
    }

      let subscriber = Observer<Any, NoError>(value: { print("輸出 \($0)") })
    
      producer.start(subscriber)
    

</pre></code>

  • 信號的第二種創建方式
    <pre><code>let (signalA, observerA) = Signal<String, NoError>.pipe()
    signalA.observeValues { (value) in
    print("輸出 (value)")
    }
    observerA.send(value: "signalA")
    </pre></code>

  • 兩個信號都發送消息的時候才會調用
    <pre><code>let (signalA, observerA) = Signal<String, NoError>.pipe()
    signalA.observeValues { (value) in
    print("輸出 (value)")
    }
    observerA.send(value: "signalA")
    let (signalB, observerB) = Signal<String, NoError>.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()
    </pre></code>

  • 實現代理
    <pre><code>let redV = RedView()
    redV.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
    view.addSubview(redV)
    redV.signal.observeValues { (value) in
    print("接收到 (value)")
    }
    </pre></code>

RedView中代碼

<pre><code>var btn : UIButton?
let (signal,obser) = Signal<Any,NoError>.pipe()

override init(frame: CGRect) {
    super.init(frame: frame)
    backgroundColor = UIColor.red
    btn = UIButton(type: .custom)
    btn?.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
    btn?.setTitle("哈哈", for: .normal)
    addSubview(btn!)
    
    btn!.reactive.controlEvents(.touchUpInside).observeValues({ (_) in
        print("redView點擊")
        self.obser.send(value: "redView代理")
        self.obser.sendCompleted()

    })

</pre></code>

  • 其他
    <pre><code>
    //nameTf 跟 pwdTf同時有值得時候才會調用
    Signal.combineLatest(nameTf.reactive.continuousTextValues, pwdTf.reactive.continuousTextValues).observeValues { (name,pwd) in
    print("(name!) ++++ (pwd!)")
    }
    //button點擊事件
    btn.reactive.controlEvents(.touchUpInside).observeValues { (_ button) in
    print(btn.tag)
    }
    //輸入框字符大于6 按鈕才可以點擊 第一種寫法
    Signal.combineLatest(nameTf.reactive.continuousTextValues,pwdTf.reactive.continuousTextValues).map { (name, password) -> Bool in
    return ((name?.characters.count)! >= 6 && (password?.characters.count)! >= 6)
    }.observeValues { (value) in
    btn.isEnabled = value
    }
    //輸入框字符大于6 按鈕才可以點擊 第二種寫法
    btn.reactive.isEnabled <~ Signal.combineLatest(nameTf.reactive.continuousTextValues,pwdTf.reactive.continuousTextValues).map { $0?.characters.count ?? 0 >= 6 && $1?.characters.count ?? 0 >= 6
    }
    //線程
    QueueScheduler.main.schedule(after: Date.init(timeIntervalSinceNow: 0.5)) {
    print("0.5秒后調用")
    }
    QueueScheduler.init().schedule(after: Date.init(timeIntervalSinceNow: 0.5)) {
    print("子線程調用")
    }
    //接收通知
    NotificationCenter.default.reactive.notifications(forName: Notification.Name(rawValue: "redView")).observeValues({ (noti) in
    print(noti.object ?? "通知")
    })
    </pre></code>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 一.UI綁定 <~ 符號用法 1.某個值動態綁定到UITextField控件上來 var firstName: M...
    Lewis海閱讀 1,191評論 1 3
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,869評論 18 139
  • By Lisa 隔三差五就會有人問我,老刀培訓的學員 Match 成功率有多少???說實話,這個問題有點難回答。因為...
    98cff7392090閱讀 710評論 0 2
  • 片尾曲響起,燈光點亮,卻鮮有人離開,在陳奕迅的歌聲和華麗中國風字幕后,居然還等到了彩蛋,湫復活接替了靈婆的位置,難...
    影秋千閱讀 992評論 2 1
  • php中的magic_quotes_gpc是配置在php.ini中的,他的作用類似addslashes(),就是對...
    人在碼途閱讀 3,280評論 0 0