RxSwift: Observable while a button holds down

后端 未结 2 1400
眼角桃花
眼角桃花 2021-02-11 02:36

How to create Observable which streams an event repeatedly while a button holds down?

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-11 03:08

    Even I was looking for a solution for your question. I got help from RxSwift slack channel.

        let button = submitButton.rx_controlEvent([.TouchDown])
          button
            .flatMapLatest { _ in
                Observable.interval(0.1, scheduler: MainScheduler.instance)
                    .takeUntil(self.submitButton.rx_controlEvent([.TouchUpInside]))
            }
            .subscribeNext{ x in print("BOOM \(x)") }
            .addDisposableTo(disposeBag)
    
    //prints BOOM 0 BOOM 1 BOOM 2 BOOM 3 BOOM 4 BOOM 5 for every 0.1 seconds
    

    And also Check Interval Documentation.Thanks to @jari of RxSwift slack channel.

提交回复
热议问题