如何使用rxSwift在5秒内禁用按钮点击?

huangapple go评论45阅读模式
英文:

How to Disable the button tap in 5 seconds by using rxSwift?

问题

**rxSwift版本**

  pod 'RxSwift', '6.2.0'        
  pod 'RxCocoa', '6.2.0'


TrueBtn.rx.tap.throttle(.seconds(5), scheduler: MainScheduler.instance)
.subscribe(onNext: { [weak self] in

print("TrueBtn-true")

}).disposed(by: bag)

如何使用`rxSwift`5秒内禁用按钮点击?

在相同的视图中有一个假按钮。

FalseBtn.rx.tap.throttle(.seconds(5), scheduler: MainScheduler.instance)
.subscribe(onNext: { [weak self] in

print("FalseBtn-false")

}).disposed(by: bag)

我点击了真按钮,然后在1秒后再次点击了真按钮。然后我点击了假按钮。
现在的结果是:

>TrueBtn-true    
等待1     
FalseBtn-false     
等待4  
TrueBtn-true 

我想要的是:

>TrueBtn-true    
等待1    
FalseBtn-false   
等待超过5   
不打印任何内容 

`参数最新: 应在自上次元素发射以来的某个时间宽窗口内接收到的最新元素被发射。`

我是否可以编写一些代码,在不更改`rx-swift` API 的情况下将最新值设置为 false
英文:

rxSwift version:

pod 'RxSwift', '6.2.0'
pod 'RxCocoa', '6.2.0'

TrueBtn.rx.tap.throttle(.seconds(5), scheduler: MainScheduler.instance)
.subscribe(onNext: { [weak self] in

print("TrueBtn-true")

}).disposed(by: bag)

How to Disable the button tap in 5 seconds by using rxSwift?

There is a false button in the same view.

FalseBtn.rx.tap.throttle(.seconds(5), scheduler: MainScheduler.instance)
.subscribe(onNext: { [weak self] in

print("FalseBtn-false")

}).disposed(by: bag)

I click the true button and after 1 second click the true button again. Then I click the false button.
Now the result is :

>TrueBtn-true
wait for 1 second
FalseBtn-false
wait 4 seconds
TrueBtn-true

I want this:

>TrueBtn-true
wait for 1 second
FalseBtn-false
wait more than 5 seconds
print nothing

parameter latest: Should latest element received in a due time wide time window since last element emission is emitted.

Can I write some code to set the latest
value to false without changing the rx-swift api?

答案1

得分: 1

这个用例对我来说有点令人困惑。我会假设它是这样的。

当用户点击“真”或“假”按钮时,他们有5秒的时间来改变想法并点击另一个按钮。一旦宽限期结束,应用将打印他们的选择并禁用进一步的点击。

一些弹珠图来演示:
用户做出选择并没有改变主意。

真:   --T-----
假:  --------
结果: -------T|

用户做出选择,在5秒内改变主意。

真:   --T---------
假:  ------F-----
结果: -----------F|

用户做出选择,并在5秒后试图改变主意。

真:   --T-----------
假:  --------F-----
结果: -------T|

以下是执行以上操作的代码:

func useCase(trueButton: Observable<Void>, falseButton: Observable<Void>, scheduler: SchedulerType) -> Observable<Bool> {
    Observable.merge(
        trueButton.map { true },
        falseButton.map { false }
    )
    .debounce(.seconds(5), scheduler: scheduler)
    .take(1)
}
英文:

The use case is a bit confusing to me. I'm going to assume it's something like.

> When the user taps either the true or false button, they have 5 seconds to change their mind and tap the other button. Once the grace period is up, the app will print their choice and disable further taps.

Some marble diagrams to demonstrate:
User makes a selection and doesn't change their mind.

true:   --T-----
false:  --------
result: -------T|

User makes a selection and changes their mind before the 5 seconds are up.

true:   --T---------
false:  ------F-----
result: -----------F|

User makes a selection and tries to change their mind after 5 seconds.

true:   --T-----------
false:  --------F-----
result: -------T|

Here is the code that will do the above:

func useCase(trueButton: Observable&lt;Void&gt;, falseButton: Observable&lt;Void&gt;, scheduler: SchedulerType) -&gt; Observable&lt;Bool&gt; {
	Observable.merge(
		trueButton.map { true },
		falseButton.map { false }
	)
	.debounce(.seconds(5), scheduler: scheduler)
	.take(1)
}

huangapple
  • 本文由 发表于 2023年2月19日 13:51:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498258.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定