简体   繁体   中英

Using Reactive cocoa to observe UITouches?

Im fairly new to reactive cocoa and im trying to include elements of FRP in the game i am trying to build. From my search online the resources and the documentation for Reactive cocoa seems very limited and most of the tutorials use the same examples!

What i want to do is, to have a RACSignal that gives a stream of values for the current touch on the view (assume no multi touch for simplicity). And then use subscribeNext to perform my actions as and when the UITouch Object gets mutated. Im having trouble setting up the RAC signal itself!

Currently im doing the following (which im not sure is the right way!)

  @interface MyView:UIView{

      UITouch *currentTouch;
      RACSignal *touchSignal;
  }
  @property(nonatomic , assign)UITouch *currentTouch;



  @implementation MyView
  @synthesize currentTouch;

  -(id)init{
     if(self = [super init]){

    }
    return self;
  }

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

   NSArray *touchArray = [touches allObjects]; 
   for(UITouch *touch in toucheArray){   
       currentTouch = touch;  
       if(!touchSignal){
           touchSignal = [RACObserve(self , currentTouch) distinctUntilChanged];
       }

    }

}

But everytime i the RACSignal tries to initialize the game crashes and i get the following:

 [MyView rac_valuesForKeyPath:observer:]: unrecognized selector sent to instance 0x2084cf90

What am i doing wrong? What is the correct way to set up a RACSignal? Also, how can i use the touchSignal in a different object (maybe the MyView Model) and use subscribe next to carry out operations in a block?

This is how I would do it, using -rac_signalForSelector: . This is more declarative, less imperative (which is where I think your bugs are coming from).

RACSignal *touchSignal = [[[self
    rac_signalForSelector:@selector(touchesBegan:withEvent:)]
    reduceEach:^(NSSet *touches, UIEvent *event) {
        return [touches anyObject];
    }]
    distinctUntilChanged]

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