二、基本使用
#import// 导入头文件
2-1、监听文本框使用
- (void)learnRACWithTextFiled{// // 直接监听 textFiled的改变// [[self.testTextField rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(id x){//// NSLog(@"%@", x);////// }];// 或者[self.testTextField.rac_textSignal subscribeNext:^(NSString* textString) {NSLog(@"%@", textString); }];}// 打印出其textFiled中的文本信息来
2-3、 监听Button事件
- (void)learnRACWithButton{ [[self.testButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(idx) {NSLog(@"按钮被点击了"); }];}
2-3、手势
- (void)learnRACWithGesture{ UITapGestureRecognizer * tap =[[UITapGestureRecognizer alloc]init];[self.view addGestureRecognizer:tap];[[tap rac_gestureSignal]subscribeNext:^(UITapGestureRecognizer * tap) { // 点击可以[[[UIApplication sharedApplication]keyWindow]endEditing:YES]; }];}
2-4、通知
-(void)learnRACWithNSNotificationCenter{// 通知可以不移除[[[NSNotificationCenter defaultCenter]rac_addObserverForName:UIKeyboardWillShowNotificationobject:nil]subscribeNext:^(NSNotification * notification) {NSLog(@"show"); }];}
2-5、定时器
-(void)learnRACWithNSTimer{NSLog(@"begin");// 1. 延迟某个时间后再做某件事[[RACScheduler mainThreadScheduler]afterDelay:2.0fschedule:^{NSLog(@"2秒之后发生的事情"); }];// 2. 每个一定长度时间做一件事[[RACSignal interval:4 onScheduler:[RACScheduler mainThreadScheduler]]subscribeNext:^(NSDate * date) {NSLog(@"每隔几秒发生的事情"); }];/*
2015-12-21 13:22:23.209 ReactiveCocoaLearn[78775:4675706] begin
2015-12-21 13:22:25.409 ReactiveCocoaLearn[78775:4675706] 2秒之后发生的事情
2015-12-21 13:22:27.213 ReactiveCocoaLearn[78775:4675706] 每隔几秒发生的事情
2015-12-21 13:22:31.211 ReactiveCocoaLearn[78775:4675706] 每隔几秒发生的事情
*/}
2-6、代理
但是有局限,只能取代没有返回值的代理方法
- (void)learnRACWithProtocol{UIAlertView* alertView = [[UIAlertViewalloc]initWithTitle:@"RAC中Protocol"message:@"UIAlertView"delegate:selfcancelButtonTitle:@"Cancel"otherButtonTitles:@"OK",nil]; [alertView show]; [[selfrac_signalForSelector:@selector(alertView:clickedButtonAtIndex:) fromProtocol:@protocol(UIAlertViewDelegate)]subscribeNext:^(RACTuple*tuple){//可以多尝试下RACTuple里的属性NSLog(@"tuple.second == %@",tuple.second);if([tuple.second isEqualToNumber:@0]) {NSLog(@"cancel"); }if([tuple.second isEqualToNumber:@1]) {NSLog(@"ok"); } }];// 更简单的方式:// [[alertView rac_buttonClickedSignal]subscribeNext:^(id x) {// //可以多尝试下RACTuple里的属性// NSLog(@"%@",x);// if([x isEqualToNumber:@0])// {// NSLog(@"Cancel");// }// if([x isEqualToNumber:@1])// {// NSLog(@"Ok");// }//// }];}
2-7、KVO
[RACObserve(self.testScrollerView, contentOffset) subscribeNext:^(id x) { NSLog(@"Offset=%@",x);}];/*2015-12-2115:06:23.689ReactiveCocoaLearn[81607:4756461] Offset=NSPoint: {0,0}2015-12-2115:06:23.689ReactiveCocoaLearn[81607:4756461] Offset=NSPoint: {0,0}2015-12-2115:06:23.711ReactiveCocoaLearn[81607:4756461] Offset=NSPoint: {0, -1}2015-12-2115:06:23.790ReactiveCocoaLearn[81607:4756461] Offset=NSPoint: {0, -1.5}2015-12-2115:06:23.870ReactiveCocoaLearn[81607:4756461] Offset=NSPoint: {0, -2} */
或是
[[self.greenViewrac_valuesAndChangesForKeyPath:@"center"options:NSKeyValueObservingOptionNew observer:nil] subscribeNext:^(idx) { NSLog(@"center===%@",x);}];/* center=== ("NSPoint: {187.5, 333.5}", { kind =1;new ="NSPoint: {187.5, 333.5}";} ) */
以上是一些RAC的基本用法,熟练这几个以后,我们很多场景都能运用自如,而且会发现RAC真的很方便。
三、RACSignal使用
其实在RAC中最核心的类RACSiganl,搞定这个类就能用ReactiveCocoa开发了。
RACSiganl:信号类,一般表示将来有数据传递,只要有数据改变,信号内部接收到数据,就会马上发出数据。
创建信号 & 激活信号 & 废弃信号
// 1.创建信号 + (RACSignal *)createSignal:(RACDisposable * (^)(id subscriber))didSubscribe// 2.订阅信号,才会激活信号. - (RACDisposable *)subscribeNext:(void (^)(id x))nextBlock// 3.发送信号 - (void)sendNext:(id)value// 4.废弃信号 RACDisposable// 创建信号RACSignal *siganl = [RACSignalcreateSignal:^RACDisposable *(id subscriber) {// block调用时刻:每当有订阅者订阅信号,就会调用block。// 发送信号[subscribersendNext:@1];// 如果不在发送数据,最好发送信号完成,内部会自动调用[RACDisposable disposable]取消订阅信号。[subscriber sendCompleted];return[RACDisposabledisposableWithBlock:^{// 销毁信号// block调用时刻:当信号发送完成或者发送错误,就会自动执行这个block,取消订阅信号。// 执行完Block后,当前信号就不在被订阅了。NSLog(@"信号销毁"); }]; }];// 订阅信号,才会激活信号.[siganlsubscribeNext:^(id x) { NSLog(@"接到数据x=%@",x); }];/*
2015-12-21 15:47:18.335 ReactiveCocoaLearn[82287:4789675] 接到数据x=1
2015-12-21 15:47:18.335 ReactiveCocoaLearn[82287:4789675] 信号销毁
*/
信号的处理
3-1、map
[[self.testTextField.rac_textSignal map:^id(NSString*textStr){ return @(textStr.length);}] subscribeNext:^(idx){ NSLog(@"x==%@",x);}];// 映射
3-2、filter
[[[self.testTextField.rac_textSignal map:^id(NSString*textStr){ return @(textStr.length);}] filter:^BOOL(NSNumber* value){ return value.integerValue >2;}] subscribeNext:^(idx){ NSLog(@"x==%@",x);}];
过滤掉一部分
3-3、delay
RACSignal *siganl = [[RACSignal createSignal:^RACDisposable *(id subscriber) {NSLog(@"realySendSignal"); [subscriber sendNext:@1]; [subscriber sendCompleted];return[RACDisposable disposableWithBlock:^{NSLog(@"discard Signal"); }];}] delay:3];NSLog(@"SubscriSiganl");[siganl subscribeNext:^(idx) {NSLog(@"recevieSiganl=%@",x);}];// 延迟3秒才接收数据/*
2015-12-21 16:33:05.326 ReactiveCocoaLearn[83488:4831881] 开始预订信号
2015-12-21 16:33:05.327 ReactiveCocoaLearn[83488:4831881] 真正发送信号
2015-12-21 16:33:05.328 ReactiveCocoaLearn[83488:4831881] 销毁信号
2015-12-21 16:33:08.621 ReactiveCocoaLearn[83488:4831881] 接收信号=1
*/
注意打印的时间,发送信号,订阅信号 的时间,再次了解下整个流程。
3-4、startWith
RACSignal *siganl = [[RACSignalcreateSignal:^RACDisposable *(id subscriber) { [subscribersendNext:@"one"]; [subscriber sendCompleted];return[RACDisposabledisposableWithBlock:^{ }];}]startWith:@"two"];[siganlsubscribeNext:^(id x) { NSLog(@"接收信号=%@",x);}];// 2015-12-21 16:38:27.160 ReactiveCocoaLearn[83642:4836850] 接收信号=two// 2015-12-21 16:38:27.162 ReactiveCocoaLearn[83642:4836850] 接收信号=one
相当于在发送某个信号之前先发送另一个信号
3-5、timeout
RACSignal *siganl = [[RACSignalcreateSignal:^RACDisposable *(id subscriber) {// 假设某个请求的时间用了几秒[[RACScheduler mainThreadScheduler]afterDelay:4schedule:^{ [subscribersendNext:@"one"]; [subscriber sendCompleted]; }];return[RACDisposabledisposableWithBlock:^{// NSLog(@"销毁信号");}];// 然后timeout就是当超过这个时间的时候就会出错}]timeout:10.0onScheduler:[RACScheduler mainThreadScheduler]];[siganlsubscribeNext:^(id x){ NSLog(@"x==%@",x);}error:^(NSError * error){// 这个地方就很容易来处理错误的时候啦NSLog(@"error==%@",[error description]);}completed:^{ NSLog(@"completed");}];
比较适合用于 请求超时的时候
3-6、take & skip & takeLast
RACSignal *siganl = [[RACSignalcreateSignal:^RACDisposable *(id subscriber) { [subscribersendNext:@"one"]; [subscribersendNext:@"two"]; [subscribersendNext:@"three"]; [subscribersendNext:@"four"]; [subscriber sendCompleted];return[RACDisposable disposableWithBlock:^{ }];}]take:2];[siganlsubscribeNext:^(id x){ NSLog(@"x==%@",x);}];//take 只接收前几次//skip 跳过前几次//takeLast 只接收最后几次/ *takeUntilBlock:takeWhileBlock:skipWhileBlock:skipUntilBlock:*/
四、进阶使用
在我们向服务器进行请求的时候,RAC为我们带来了诸多方便的事情,值得探索。
此处还是用DeveloperLx的例子,textFiled举例说明。
4-1、throttle
[[self.testTextField.rac_textSignal throttle:0.5]subscribeNext:^(idx){ NSLog(@"%@", x);}];
就是在我们设置那个时间内(0.5秒),不会发送消息,让其不会一直不断的发送过来。
4-2 distinctUntilChanged
[[[self.testTextField.rac_textSignal throttle:0.5] distinctUntilChanged]subscribeNext:^(idx){ NSLog(@"%@", x);}];
相同的就不发送,直到有所该变再发送
4-3 ignore
[[[[self.testTextField.rac_textSignal throttle:0.5] distinctUntilChanged] ignore:@""] subscribeNext:^(idx){ NSLog(@"%@", x);}];
忽略某个值,像上面就是忽略 空值
4-4 switchToLatest
先综合了下 map
[[[[[[self.testTextField.rac_textSignal throttle:0.5] distinctUntilChanged] ignore:@""] map:^id(idvalue){ return [RACSignal createSignal:^RACDisposable*(idsubscriber){ [subscriber sendNext:value];[subscriber sendCompleted];return [RACDisposable disposableWithBlock:^{}];}];}]switchToLatest ]subscribeNext:^(NSString* x){ NSLog(@"x==%@", x);}];
只执行最后一次,这个地方有待推敲,暂时还不是很理解
4-5 merge
RACSignal * signalA = [RACSignal createSignal:^RACDisposable *(idsubscriber){dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(2* NSEC_PER_SEC)),dispatch_get_main_queue(),^{ [subscribersendNext:@"Signal_A"];[subscribersendCompleted];});return nil;}];RACSignal * signalB = [RACSignal createSignal:^RACDisposable *(idsubscriber){dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(3* NSEC_PER_SEC)),dispatch_get_main_queue(),^{ [subscribersendNext:@"Signal_B"];[subscribersendCompleted];});return nil;}];NSLog(@"开始预订");[[RACSignal merge:@[signalA, signalB]]subscribeNext:^(idx) { NSLog(@"x==%@",x);}];/*
2015-12-21 17:54:24.105 ReactiveCocoaLearn[85576:4905054] 开始预订
2015-12-21 17:54:26.306 ReactiveCocoaLearn[85576:4905054] x==Signal_A
2015-12-21 17:54:27.398 ReactiveCocoaLearn[85576:4905054] x==Signal_B
*/
同时订阅信号
4-6 concat
NSLog(@"开始预订");[[RACSignal concat:@[signalA, signalB]]subscribeNext:^(id x) { NSLog(@"x==%@",x);}];/*2015-12-2117:57:03.718ReactiveCocoaLearn[85651:4908056] 开始预订2015-12-2117:57:05.720ReactiveCocoaLearn[85651:4908056] x==Signal_A2015-12-2117:57:09.012ReactiveCocoaLearn[85651:4908056] x==Signal_B*/
执行完A 后才执行 B ,而且A必须成功,B才会执行,他们是异步请求.
4-7、zipwith
NSLog(@"开始预订");[[signalA zipWith:signalB]subscribeNext:^(id x) { NSLog(@"x==%@",x);}]; /*2015-12-2118:01:18.770ReactiveCocoaLearn[85742:4913279]开始预订2015-12-2118:01:22.071ReactiveCocoaLearn[85742:4913279]x== ("Signal_A","Signal_B") */
注意看上面返回的时间差距
返回一个RACTuple(元祖) ,A、B 至少都发送过一次消息后,才返回。
三者以上的可以用下面这个,combineLatest,同上
[[RACSignal combineLatest:@[signalA,signalB,signalC]] subscribeNext:^(idx){ NSLog(@"x==%@",x);}];
五、RAC常见宏
5.1RAC(TARGET, [KEYPATH, [NIL_VALUE]]):用于给某个对象的某个属性绑定
RAC(self.testButton,backgroundColor)= [RACObserve(self.testButton,selected)map:^UIColor *(NSNumber *selected){ return [selectedboolValue]? [UIColor redColor] : [UIColor greenColor];}];[[self.testButtonrac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(UIButton*btn){btn.selected= !btn.selected;}];
直接改变button 的颜色
5.2RACObserve(self, name):监听某个对象的某个属性,返回的是信号
[RACObserve(self.greenView, center) subscribeNext:^(idx) { NSLog(@"%@",x);}];
点击按钮,改变其center之后
/*2015-12-2118:18:52.229ReactiveCocoaLearn[86031:4931305] NSPoint: {0,0}2015-12-2118:18:54.024ReactiveCocoaLearn[86031:4931305] 按钮被点击了2015-12-2118:18:54.025ReactiveCocoaLearn[86031:4931305] NSPoint: {187.5,333.5}*/
下面这个也是同样的用这个宏的,这是用最少的代码写一个秒表。
RAC(self.testLabel, text) =[[RACSignal interval:1 onScheduler:[RACScheduler mainThreadScheduler]]map:^NSString *(NSDate * date) {returndate.description;}];