带rxSwift的简单计时器

我正在尝试使用Rx Swift重现一个简单的计时器.

我有一个暂停/播放按钮,只适用于暂停和恢复.

gameTimer = Observable<NSInteger>.interval(1,scheduler: MainScheduler.instance)
            .subscribeNext({ sec -> Void in
            print("\(sec) s")
        }).adddisposableto(disposeBag!)


            pauseResumeButton.rx_tap
            .map{ !self.isRunning.value }
            .startWith(true)
            .bindTo( isRunning )
            .adddisposableto(disposeBag!)

isRunning显然是一个变量.
我可以停止计时器设置disposeBag = nil但是我不能暂停它…. itried过滤事件但计数显然继续增长…

谁能帮我?谢谢!

一个小技巧:
class ViewController: UIViewController {

    let isRunning = Variable(true)

    override func viewDidLoad() {
        super.viewDidLoad()

        isRunning.asObservable()
            .debug("isRunning")
            .flatMapLatest {  isRunning in
                isRunning ? Observable<Int>.interval(1,scheduler: MainScheduler.instance) : .empty()
            }
            .flatMapWithIndex { (int,index) in Observable.just(index) }
            .debug("timer")
            .subscribe()
            .adddisposableto(disposeBag)

        navigationItem.rightBarButtonItem!.rx.tap
            .subscribe(onNext: { [uNowned self] in
                self.isRunning.value = !self.isRunning.value
            })
            .adddisposableto(disposeBag)

    }
}

结果:

2016-12-17 20:53:11.408: timer -> subscribed
2016-12-17 20:53:11.414: isRunning -> subscribed
2016-12-17 20:53:11.415: isRunning -> Event next(true)
2016-12-17 20:53:12.503: timer -> Event next(0)
2016-12-17 20:53:13.488: timer -> Event next(1)
2016-12-17 20:53:14.488: timer -> Event next(2)
2016-12-17 20:53:15.488: timer -> Event next(3)
2016-12-17 20:53:16.154: isRunning -> Event next(false)
2016-12-17 20:53:23.155: isRunning -> Event next(true)
2016-12-17 20:53:24.223: timer -> Event next(4)
2016-12-17 20:53:25.229: timer -> Event next(5)
2016-12-17 20:53:26.228: timer -> Event next(6)
2016-12-17 20:53:27.220: timer -> Event next(7)
2016-12-17 20:53:28.225: timer -> Event next(8)
2016-12-17 20:53:29.221: timer -> Event next(9)
2016-12-17 20:53:29.971: isRunning -> Event next(false)

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...