框架學(xué)習(xí)1.1RxSwift-定時器

1.常規(guī)步驟,RX的pod導(dǎo)入
參看:http://www.itdecent.cn/p/ba33981cf597
2.創(chuàng)建UI序列

 let isRunning = Observable
            .merge([startBtn.rx.tap.map({return true}), stopBtn.rx.tap.map({return false})])
            .startWith(false)
            .share(replay: 1, scope: .whileConnected)

let isNotRunning = isRunning
            .map({running -> Bool in
                print(running)
                return !running
            })
            .share(replay: 1, scope: .whileConnected)

3.UI序列響應(yīng)綁定

      isRunning.subscribe(onNext:{
            print($0)
        })
            .disposed(by: disposBag)

    isRunning
            .bind(to: stopBtn.rx.isEnabled)
            .disposed(by: disposBag)
        
        isNotRunning
            .bind(to: splitBtn.rx.isHidden)
            .disposed(by: disposBag)
        
        isNotRunning
            .bind(to: startBtn.rx.isEnabled)
            .disposed(by: disposBag)

4.創(chuàng)建定時器序列

  timer = Observable<Int>
            .interval(0.1, scheduler: MainScheduler.instance)
            .withLatestFrom(isRunning, resultSelector: {_, running in running})
            .filter({runing in runing})
            .scan(0, accumulator: {(acc, _) in
                return acc+1
            })
            .startWith(0)
            .share(replay: 1, scope: .whileConnected)

5.定時器序列事件響應(yīng)綁定

        timer
            .subscribe{ (mecs) in
                print("\(mecs)00ms")
            }
            .disposed(by: disposBag)
        
        
        timer.map(stringFromTimeInterval)
            .bind(to: self.topLab.rx.text)
            .disposed(by: disposBag)

6.其他序列的創(chuàng)建和綁定流程

        let lapsSequence = timer
            .sample(splitBtn.rx.tap)
            .map(stringFromTimeInterval)
            .scan([String](), accumulator: { lapTimes, newTime in
                return lapTimes + [newTime]
            })
            .share(replay: 1, scope: .whileConnected)
        
        lapsSequence
            .bind(to: scView.rx.items(cellIdentifier: "Cell", cellType: UITableViewCell.self)) { (row, element, cell) in
                cell.textLabel?.text = "\(row+1)) \(element)"
            }
            .disposed(by: disposBag)

7.實現(xiàn)效果

1.png

代碼地址:https://gitee.com/xgkp/Rx1point1.git

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容