2 RACCommands 以便一个被禁用而另一个正在执行,反之亦然
2 RACCommands so that one is disabled while other is executing and vice versa
我如何创建 2 个 RACCommand
以便在另一个执行时禁用一个并且反之亦然?
像这样,
_prevTrackCommand = [[RACCommand alloc] initWithEnabled: [_nextTrackCommand.executing not] signalBlock:^RACSignal *(id _) {}];
_nextTrackCommand = [[RACCommand alloc] initWithEnabled: [_prevTrackCommand.executing not] signalBlock:^RACSignal *(id _) {}];
但是此代码将不起作用,因为 _nextTrackCommand
在 _prevTrackCommand
初始化时 nil
。
您可以使用可以作为 RACSignal
但允许您手动转发事件的 RACSubject
:
RACSubject* commandAActive = [RACSubject subject];
RACSubject* commandBActive = [RACSubject subject];
RACCommand* commandA = [[RACCommand alloc] initWithEnabled:[commandBActive not]
signalBlock:^RACSignal * _Nonnull(id _Nullable input) {
// block
}];
RACCommand* commandB = [[RACCommand alloc] initWithEnabled:[commandAActive not]
signalBlock:^RACSignal * _Nonnull(id _Nullable input) {
// block
}];
[commandA.executing subscribeNext:^(NSNumber * _Nullable x) {
[commandAActive sendNext:x];
}];
[commandB.executing subscribeNext:^(NSNumber * _Nullable x) {
[commandBActive sendNext:x];
}];
我如何创建 2 个 RACCommand
以便在另一个执行时禁用一个并且反之亦然?
像这样,
_prevTrackCommand = [[RACCommand alloc] initWithEnabled: [_nextTrackCommand.executing not] signalBlock:^RACSignal *(id _) {}];
_nextTrackCommand = [[RACCommand alloc] initWithEnabled: [_prevTrackCommand.executing not] signalBlock:^RACSignal *(id _) {}];
但是此代码将不起作用,因为 _nextTrackCommand
在 _prevTrackCommand
初始化时 nil
。
您可以使用可以作为 RACSignal
但允许您手动转发事件的 RACSubject
:
RACSubject* commandAActive = [RACSubject subject];
RACSubject* commandBActive = [RACSubject subject];
RACCommand* commandA = [[RACCommand alloc] initWithEnabled:[commandBActive not]
signalBlock:^RACSignal * _Nonnull(id _Nullable input) {
// block
}];
RACCommand* commandB = [[RACCommand alloc] initWithEnabled:[commandAActive not]
signalBlock:^RACSignal * _Nonnull(id _Nullable input) {
// block
}];
[commandA.executing subscribeNext:^(NSNumber * _Nullable x) {
[commandAActive sendNext:x];
}];
[commandB.executing subscribeNext:^(NSNumber * _Nullable x) {
[commandBActive sendNext:x];
}];