(环境 OS X 10.11.5 || Xcode 7.3.1 || ReactiveCocoa 4.1.0)
案例一、 登录界面的一些信号和响应
首先 来看看 textfield的 内容发生改变是的响应方法
//获取 textfield 的内容改变信号 并且订阅
/*当textfield中的内容发生改变的时候回触发subscribeNext(订阅)block*/
[self.firstTextfield.rac_textSignal subscribeNext:^(id x) {
NSLog(@"%@",x);
}];
1、已通过map函数将型号转换成新的信号
2、将多个信号联合起来
3、将联合起来的信号来给按钮的 额那边了属性赋值
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *firstTextfield;
@property (weak, nonatomic) IBOutlet UITextField *secondTextfield;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//获取 textfield 的内容改变信号 并且订阅
/*当textfield中的内容发生改变的时候回触发subscribeNext(订阅)block*/
[self.firstTextfield.rac_textSignal subscribeNext:^(id x) {
NSLog(@"%@",x);
}];
//将 textfield 输入信号的 返回值进行修改 得到新的信号!
RACSignal *firstSignal = [self.firstTextfield.rac_textSignal map:^id(NSString *firstString) {
NSInteger length = firstString.length;
if (length >= 5 && length <= 10) {
return @(YES);
}
return @(NO);
}];
RACSignal *secondSignal = [self.secondTextfield.rac_textSignal map:^id(NSString *secondString) {
if (secondString.length >5 && secondString.length < 10) {
return @(YES);
}
return @(NO);
}];
// 绑定用户名、密码判断结果的三个信号量,如果都为真,则按钮可用
RAC(self.loginButton,enabled) = [RACSignal combineLatest:@[firstSignal,secondSignal] reduce:^(NSNumber *firstRes,NSNumber *secondRes){
return @(firstRes.boolValue && secondRes.boolValue);
}];
}
- (IBAction)login:(UIButton *)sender
{
NSLog(@"***** 点击按钮 *****");
}
案例二 RAC下的观察者模式
1、【RACObserve()】使用了KVO来监听property的变化,只要 被观察属性 被自己或外部改变,block就会被执行。
2、该property必须支持KVO
3、 使用必须在注册之后
@property(nonatomic,copy)NSString *textString;
self.textString = @"123";
self.textString = @"456";
self.textString = @"ddd";
[RACObserve(self, textString) subscribeNext: ^(NSString *newString){
NSLog(@"newString = %@", newString);
}];
self.textString = @"ads";
self.textString = @"adsmmmm";
RAC 下的通知
//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];
案例三 taget - action
//button
UIButton *bu = [UIButton buttonWithType:UIButtonTypeCustom];
bu.frame = CGRectMake(20, 400, 300, 40);
bu.backgroundColor = [UIColor orangeColor];
[self.view addSubview:bu];
[[bu rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(id x) {
NSLog(@"***** 响应RAC button的点击 *****");
}];
//手势
self.view.userInteractionEnabled = YES;
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]init];
[[tap rac_gestureSignal] subscribeNext:^(UITapGestureRecognizer * tap) {
NSLog(@"***** 响应单击手势 *****");
}];
[self.view addGestureRecognizer:tap];
案例4 延时
[[RACScheduler mainThreadScheduler]afterDelay:2 schedule:^{
NSLog(@"***** first 延时rac写法 *****");
}];
[[RACSignal interval:1 onScheduler:[RACScheduler mainThreadScheduler]]subscribeNext:^(NSDate * date) {
NSLog(@"***** second 延时rac写法 *****");
}];
案例5 多个请求都至少完成一次 然后出发一个总方法
// 处理多个请求,都返回结果的时候,统一做处理.
RACSignal *request1 = [RACSignal createSignal:^RACDisposable *(id subscriber) {
// 发送请求1
[subscriber sendNext:@"发送请求1"];
return nil;
}];
RACSignal *request2 = [RACSignal createSignal:^RACDisposable *(id subscriber) {
// 发送请求2
[subscriber sendNext:@"发送请求2"];
return nil;
}];
// 使用注意:几个信号,参数一的方法就几个参数,每个参数对应信号发出的数据。
[self rac_liftSelector:@selector(totalFuctonR1:R2:) withSignalsFromArray:@[request1,request2]];
-(void)totalFuctonR1:(id)data1 R2:(id)data2{
NSLog(@"总方法触发:data1 = %@ ----- data2 = %@",data1,data2);
}
案例6 将多个不同类型的数据组合成一个元组
// 把参数中的数据包装成元组
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);
案例7 使用 #define RAC(TARGET, ...)宏 (通过这个可以将_label的text属性和后面的信号绑定信号内容发生改变的时候自动更新)
RAC(_label,text) = self.textField.rac_textSignal;