简体   繁体   中英

ReactiveCocoa and UISwitch

//@property (weak, nonatomic) IBOutlet UIImageView *imageView;
//@property (nonatomic) UIImage *image;
//@property (nonatomic) PhotoEffect *effect;
//@property (weak, nonatomic) IBOutlet UISwitch *glossSwitch;

Currently, I have a problem due to UISwitch doesn't work with KVO . The code below triggers only if switch was changed from its initial state:

RAC(self.imageView, image) = [[[[RACSignal

                                combineLatest:@[ RACObserve(self, image), [self.glossSwitch
                                                                                   rac_signalForControlEvents:UIControlEventValueChanged], RACObserve(self, effect)]]
                              deliverOn:[RACScheduler scheduler]]
                              reduceEach:^UIImage *(UIImage *im, UISwitch *glossSwitch, PhotoEffect *effect) {
                                  if (!im) {
                                      return nil;
                                  }
                                  if (effect) {
                                      im = [im imageWithEffect:effect.type];
                                  }

                                  if (glossSwitch.on) {
                                      im = [GlossyIcon applyShineToImage:im];
                                  }
                                  return im;
                              }]
                              deliverOn:RACScheduler.mainThreadScheduler];

-combineLatest: accumulates one "next" item from each signal in the array until all the signals have sent one. At that point, it finally sends a RACTuple containing the "next" value from each signal in the array.

Your RACObserve signals send one "next" each upon initial setup. They'll send "next"s again in the future when the properties change.

The UISwitch is sending a "next" as expected based on the control event. But since the RACObserve signals have likely stopped sending "next"s, they leave your UISwitch hanging and -combineLatest: can't send more than that first "next". So your -reduceEach: only fires that first time.

EDIT: Actually, hang on - I just reread the docs for -combineLatest: and it says that once the first full set of "next"s are provided (for each signal), any additional "next"s from any signal should cause the combined signal to pass a RACTuple with the latest values from each. So I'm not exactly sure what's going on, sorry for the non-answer!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM