Cocoapoas Import
////testview.m//Reactivecocoademo////Created by Dllo on 16/4/1.//copyright©2016 year Haiteng. All rights reserved.//#import "TestView.h"@implementationTestView-(Instancetype) initWithFrame: (cgrect) frame{ Self=[Super Initwithframe:frame]; if(self) {UIButton*button =[UIButton Buttonwithtype:uibuttontypesystem]; Button.frame= CGRectMake (Ten,Ten,280,280); Button.backgroundcolor=[Uicolor Yellowcolor]; [Self Addsubview:button]; [Button addtarget:self action: @selector (buttonaction:) forcontrolevents:uicontroleventtouchupinside]; } returnSelf ;}- (void) Buttonaction: (UIButton *) sender{NSLog (@"Click the button");}@end
////VIEWCONTROLLER.M//Reactivecocoademo////Created by Dllo on 16/4/1.//copyright©2016 year Haiteng. All rights reserved.//#import "ViewController.h"#import "TestView.h"#import "AFNetWorking.h"#import "ReactiveCocoa.h"@interfaceViewcontroller () @property (nonatomic, assign) Nsinteger count;@end@implementationViewcontroller/**REACTIVECOCOA Responsive programming abbreviation RAC does not consider the calling process, direct attention to event results support iOS and OS systems also have called RAC Function-responsive Programming (FRP) * Functional Programming Example: Masonry*/- (void) viewdidload {[Super viewdidload]; Self.view.backgroundColor=[Uicolor Graycolor]; /*the core class in the Racsignal RAC is typically implemented by data changes, which send a signal when it is passed, and if the "subscription signal" is received at this time. */[self replacedelegate]; [Self REPLACEKVO]; }#pragmaMark-rac Implementation Agent-(void) replacedelegate{TestView*testview = [[TestView alloc] Initwithframe:cgrectmake (0,0, -, -)]; Testview.backgroundcolor=[Uicolor Blackcolor]; [Self.view Addsubview:testview]; //as long as TestView calls the Buttonaction: method, it turns into a signal.Racsignal *signal =[TestView rac_signalforselector: @selector (buttonaction:)]; //Subscribe to Signals__weak Viewcontroller *temp =Self ; [Signal Subscribenext:^(IDx) {NSLog (@"Response Signal"); Temp.count++; }];}#pragmaMark-rac kvo-(void) replacekvo{/**self is called because the object being observed is the property of the current class. * The properties observed by the first parameter do not misspell*/ //I'm listening for changes to my Count property//racsignal *signal = [self rac_valuesforkeypath:@ "count" Observer:nil]; //(This method, both the old and new values are desirable)Racsignal *signal = [Self Rac_valuesandchangesforkeypath:@"Count"Options:nskeyvalueobservingoptionnew |Nskeyvalueobservingoptionold Observer:nil]; //a subscription signal (whether it is a signal returned with a RAC, or a signal that comes with it, or a signal created by itself must be an object)[Signal subscribenext:^ (IDx) {NSLog (@"%@", x); }];}@end
This print will see
#pragma mark-Monitor events
-(void) observeevent{ // control that inherits from Uicontrol can use rac_signalforcontrolevents: Method Listener Event [[_button rac_signalforcontrolevents:uicontroleventtouchupinside] subscribenext:^ (ID x) { NSLog (@ " all control controls UIControlEventTouchUpInside This mode can listen to their events " ); }]; }
Call this method in Viewdidload to see.
#pragma Mark-textfiel Related
- (void) textfieldtextchange{//rac_textsignal Get the change signal of text[_textfiled.rac_textsignal subscribenext:^ (IDx) {NSLog (@"%@", x); }]; //text string length greater than n starts responding//[[_textfiled.rac_textsignal Filter:^bool (NSString *value) {// //return value.length > 6;//}] subscribenext:^ (id x) {//NSLog (@ "%@", x);// }];=// // //Map Map//[[[ _textfiled.rac_textsignal Filter:^bool (nsstring *value) {//return value.length > 6;//}] Map:^id (NSString *value) {//return [NSString stringwithformat:@ "Tomorrow Holiday%@", value];//}] subscribenext:^ (id x) {//NSLog (@ "?? %@ ", x);// }]; // //throttle:<# (nstimeinterval) #> throttling. Delay response (response when the signal does not change in 3 seconds)//[[[Self.textFiled.rac_textSignal Throttle:3] distinctuntilchanged] subscribenext:^ (id x) {//NSLog (@ "%@", x);// }]; }
You can call this method in Viewdidload to view it.
#pragma mark-Multiple data requests
-(void) multiplyrequest{racsignal*firstsignal = [racsignal createsignal:^racdisposable * (ID<RACSubscriber>subscriber) { //processing Signal[Apptools Getwithurl:@"http://c.3g.163.com/nc/ad/headline/0-4.html"Par:nil success:^ (IDresponseobject) {NSLog (@"The first request is complete"); //Send Signal[subscriber Sendnext:responseobject]; } Filed:^ (Nserror *error) { }]; returnNil; }]; Racsignal*secondsignal = [racsignal createsignal:^racdisposable * (ID<RACSubscriber>subscriber) { //processing Signal[Apptools Getwithurl:@"http://c.3g.163.com/nc/article/headline/T1348647853363/0-140.html"Par:nil success:^ (IDresponseobject) {NSLog (@"A second request is completed"); //Send Signal[subscriber Sendnext:responseobject]; } Filed:^ (Nserror *error) { }]; returnNil; }]; //response when all two signals emit content[Self rac_liftselector: @selector (requestwithfirst:seconddic:) withsignals:firstsignal,secondsignal, nil]; }- (void) Requestwithfirst: (Nsdictionary *) firstdic seconddic: (nsdictionary*) sencondic{NSLog (@"all return values.");}
This method can be called in viewdidload to view.
// binds a property of an object to a signal that, when the signal changes, assigns the contents of the signal to the bound object property RAC (_lable,text) = _textfiled.rac_textsignal;
Reactivecocoa-Responsive Programming RAC