将ReactiveCocoa中的信号组合到一个新的信号中,当所有信号都发生变化时触发 [英] Combine signals in ReactiveCocoa to a new one that fires when all change
问题描述
我正在尝试学习ReactiveCocoa,我正在编写一个简单的Space Invaders克隆,基于Ray Wenderlich教程。
最近在开发过程中,我遇到了一个无法解决的问题。
基本上我有两个信号:
I'm trying to learn ReactiveCocoa and I'm writing a simple Space Invaders clone, based on a Ray Wenderlich tutorial. Lately during the development, I faced an issue I can't resolve. Basically I've two signals:
- 一个轻拍手势信号
- a timed每秒触发的序列
我想要实现的是将这些信号组合成一个新信号,当两个信号发生变化时会触发这些信号:
有可能吗?
我看到了combineLatest方法,但只要有任何信号改变就会执行该块。
What I want to achieve is to combine these signals in a new one, that fires when both the signals change: is it possible? I saw the combineLatest method, but the block is execute whenever any signals change.
我想要的伪代码是:
RACSignal *updateEventSignal = [RACSignal interval:1 onScheduler:[RACScheduler mainThreadScheduler]];
RACSignal *gestureSignal = [[UITapGestureRecognizer new] rac_gestureSignal];
[[RACSignal combineBoth:@[gestureSignal, updateEventSignal]
reduce:^id(id tap, id counter){
return tap;
}]
subscribeNext:^(id x) {
NSLog(@"Tapped [%@]", x);
}];
可能我可以通过其他方式获得相同的结果,或者这不是预期的行为或ReactiveCocoa,但是在这一点上,我想知道我是否在正确的被动轨道上。
Probably I can achieve the same result in other way or this is not the expected behaviour or ReactiveCocoa, but at this point I wonder if I'm in the right reactive track or not.
推荐答案
+ combineLatest:reduce:
,你想要 + zip:reduce:
。 Zip要求在减少和发送新值之前更改所有信号。
Instead of +combineLatest:reduce:
, you want +zip:reduce:
. Zip requires that all the signals change before reducing and sending a new value.
这篇关于将ReactiveCocoa中的信号组合到一个新的信号中,当所有信号都发生变化时触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!