1.此处使用https://github.com/ReactiveCocoa/ReactiveObjC.git
platform :ios,'8.0'
target 'RacDemo' do
pod 'ReactiveObjC'
end
2.使用
1)引入头文件
#import <ReactiveObjC.h>
2)文本输入框监听
UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, 36)];
field.text = @"placeholder";
field.backgroundColor = [UIColor cyanColor];
[self.viewaddSubview:field];
[field.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
NSLog(@"%@",x);// 第一次返回为已经赋值的文本
}];
3)多条件判断
RACSignal*signalFirst = [field.rac_textSignalmap:^id_Nullable(NSString*_Nullablevalue) {
if(value.length>5&& value.length<10) {
return@(YES);
}
return@(NO);
}];
RACSignal*signalSecond = [fieldSecond.rac_textSignalmap:^id_Nullable(NSString*_Nullablevalue) {
if(value.length>5&& value.length<10) {
return@(YES);
}
return@(NO);
}];
RAC(btn,enabled) = [RACSignalcombineLatest:@[signalFirst, signalSecond]reduce:^(NSNumber*valueFirst,NSNumber*valueSecond){
return@(valueFirst.boolValue&& valueSecond.boolValue);
}];
4)绑定label和textFiled
_label= [[UILabelalloc]initWithFrame:CGRectMake(0,151+16+(36+16)*2,self.view.bounds.size.width,36)];
[self.view addSubview:_label];
RAC(_label,text) = self.textField.rac_textSignal;
5)kvo
self.textString = @"123";
self.textString = @"456";
self.textString = @"ddd";// 这里开始返回
[RACObserve(self,textString)subscribeNext: ^(NSString*newString){
NSLog(@"newString = %@", newString);
}];
self.textString = @"ads";
self.textString = @"adsmmmm";
6)nofication
//NSNotification 添加信号
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"MYNOTIFICATION" object:nil] subscribeNext:^(NSNotification *noti) {
NSLog(@"***** Notification Received %@ *****",noti.userInfo[@"data"]);
}];
NSNotification *noti1 = [[NSNotification alloc]initWithName:@"MYNOTIFICATION" object:nil userInfo:@{@"data":[NSString stringWithFormat:@"%d",arc4random()%100]}];
[[NSNotificationCenter defaultCenter] postNotification:noti1];
NSNotification *noti2 = [[NSNotification alloc]initWithName:@"MYNOTIFICATION" object:nil userInfo:@{@"data":[NSString stringWithFormat:@"%d",arc4random()%100]}];
[[NSNotificationCenter defaultCenter] postNotification:noti2];
7)target_action
UIButton *bu = [UIButton buttonWithType:UIButtonTypeCustom];
bu.frame=CGRectMake(0,151+16+36+16,self.view.bounds.size.width,36);
bu.backgroundColor = [UIColor orangeColor];
[self.viewaddSubview:bu];
[[burac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
NSLog(@"***** 响应RAC button的点击 *****");
}];
//手势
self.view.userInteractionEnabled = YES;
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] init];
[[taprac_gestureSignal] subscribeNext:^(UITapGestureRecognizer * tap) {
NSLog(@"***** 响应单击手势 *****");
}];
[self.view addGestureRecognizer:tap];
8)延迟
[[RACScheduler mainThreadScheduler] afterDelay:2 schedule:^{
NSLog(@"***** first 延时rac写法 *****");
}];// 单次
[[RACSignal interval:1 onScheduler:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSDate * date) {
NSLog(@"***** second 延时rac写法 *****");
}];// 重复
9)组
- (void)group {
// 处理多个请求,都返回结果的时候,统一做处理.
RACSignal *request1 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
// 发送请求1
[subscribersendNext:@"发送请求1"];
returnnil;
}];
RACSignal *request2 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
// 发送请求2
// [subscriber sendNext:@"发送请求2"];
returnnil;
}];
// 使用注意:几个信号,参数一的方法就几个参数,每个参数对应信号发出的数据。
[selfrac_liftSelector:@selector(totalFuctonR1:R2:)withSignalsFromArray:@[request1,request2]];
}
-(void)totalFuctonR1:(id)data1 R2:(id)data2{
NSLog(@"总方法触发:data1 = %@ ----- data2 = %@",data1,data2);
}
10)元组
// 把参数中的数据包装成元组
RACTuple *tuple = RACTuplePack(@"xmg",@20,@"m",@(999),@[@"a"],@{@"key":@"value"});
RACTupleUnpack(NSString *name,NSNumber *age,NSString *sex,NSNumber *price,NSArray *arr,NSDictionary *dic) = tuple;
NSLog(@"name:%@ age:%@ sex:%@ price:%@ arr:%@ dic:%@",name,age,sex,price,arr,dic);
网友评论