标签:
上一篇讲ReactiveCocoa是函数响应式编程,并将多种事件响应的方式统一起来,使得不同的事件响应方式高度统一。同时也讲了ReactiveCocoa框架里面常见的几个概念。接下来基于那几个概念来看看UI开发中的几个应用。
1 [[self.btn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(id x) { 2 NSLog(@"点击了button"); 3 }];
到rac_signalForControlEvents方法里面查看这个方法做的操作:
1 - (RACSignal *)rac_signalForControlEvents:(UIControlEvents)controlEvents { 2 @weakify(self); 3 4 return [[RACSignal 5 createSignal:^(id<RACSubscriber> subscriber) { 6 @strongify(self); 7 8 [self addTarget:subscriber action:@selector(sendNext:) forControlEvents:controlEvents]; 9 [self.rac_deallocDisposable addDisposable:[RACDisposable disposableWithBlock:^{ 10 [subscriber sendCompleted]; 11 }]]; 12 13 return [RACDisposable disposableWithBlock:^{ 14 @strongify(self); 15 [self removeTarget:subscriber action:@selector(sendNext:) forControlEvents:controlEvents]; 16 }]; 17 }] 18 setNameWithFormat:@"%@ -rac_signalForControlEvents: %lx", RACDescription(self), (unsigned long)controlEvents]; 19 }
可以看到创建了一个RACSignal信号并在block回调的时候执行操作为按钮添加了监听。此方法返回一个RACSignal信号,同时我们在外界调用subscribeNext订阅这个信号,当点击按钮的时候调用sendNext方法发送值出来就回调了subscribeNext的block。
1 [self.myTextView.rac_textSignal subscribeNext:^(id x) { 2 NSLog(@"输出:%@",x); 3 }];
1 UITapGestureRecognizer *tap = [UITapGestureRecognizer new]; 2 [self.redView addGestureRecognizer:tap]; 3 [tap.rac_gestureSignal subscribeNext:^(id x) { 4 NSLog(@"点击了红色的view"); 5 }];
可以感到一些比较平常的UI控件基于 ReactiveCocoa上使用起来还是比较简单的,这里值得说一下的是当UI控件是代理方式来监听响应过程的时候。比如UIImagePicker。下面代码实现一个简单的小功能,点击按钮选择图片,图片选择好了之后显示在UIImageView上面。
1 [[self.btn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(id x) { 2 3 //点击按钮弹出UIImagePicker 4 self.imagePicker = [UIImagePickerController new]; 5 [self.imagePicker.rac_imageSelectedSignal subscribeNext:^(id x) { 6 //该block回调是在照片选择完成的时候调用 7 NSLog(@"%@",x); 8 NSDictionary *dic = (NSDictionary *)x; 9 self.myImageView.image = dic[@"UIImagePickerControllerOriginalImage"]; 10 [self.imagePicker dismissViewControllerAnimated:YES completion:nil]; 11 }]; 12 //rac_delegateproxy是RAC下的代理属性,这行代码可以理解为,RAC下的代理将会执行block回调替换之前的代理去执行imagePickerControllerDidCancel方法
13 [[self.imagePicker.rac_delegateProxy signalForSelector:@selector(imagePickerControllerDidCancel:)] subscribeNext:^(id x) {
14 //该block调用时候:当delegate要执行imagePickerControllerDidCancel
15 [self.imagePicker dismissViewControllerAnimated:YES completion:nil];
16 }];
17
18 [self presentViewController:self.imagePicker animated:YES completion:nil]; 19 20 21 }];
第一个页面注册通知:
1 [[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"ChangeColor" object:nil] subscribeNext:^(id x) { 2 NSNotification *notification = (NSNotification *)x; 3 NSLog(@"收到通知:%@",notification.object); 4 self.view.backgroundColor = (UIColor *)notification.object; 5 }];
第二个页面中返回按钮发送通知:
[[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeColor" object:[UIColor grayColor]];
currentValue是视图控制器拥有的一个Int类型的属性.观察该属性的变化
[[self rac_valuesAndChangesForKeyPath:@"currentValue" options:(NSKeyValueObservingOptionNew) observer:self] subscribeNext:^(id x) { //解包元组,会把元组里面的值按顺序给变量赋值 RACTupleUnpack(NSString *kind,NSString *new) = x; NSLog(@"观察到currentValue的值发生改变,现在的value等于%@,%@",kind,new); }];
按钮点击改变currentValue的值
[[self.valueButton rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(id x) { self.currentValue ++; }];
可以看到RAC下的UI高度统一了多种事件响应成block回调的方式。文章不精髓,只希望能够记下自己学习的点滴并以最简单的形式分享出来。若有不当之处,请指出。
标签:
原文地址:http://www.cnblogs.com/develop-SZT/p/5292622.html