稀土 2017-11-29
RAC(ReactiveCocoa)使用方法(一)
RAC(ReactiveCocoa)使用方法(二)
上篇文章:RAC(ReactiveCocoa)使用方法(一)
中主要介绍了一些RAC中常见类的用法,这篇文章主要总结日常开发中结合一些UI控件的用法。
RAC给常见的很多UI类拓展了用法,使得开发变得越来越简单,减少了很多不必要的代理和Target代码,RAC内部已经处理好了这些事件。
---
贴上核心代码,具体代码见Github;
// // ViewModel.m // // // Created by soliloquy on 2017/11/28. // //豆瓣电影API #define url @"https://api.douban.com/v2/movie/in_theaters?apikey=0b2bdeda43b5688921839c8ecb20399b&city=%E5%8C%97%E4%BA%AC&start=0&count=100&client=&udid=" #import "ViewModel.h" #import <AFNetworking/AFNetworking.h> #import <MJExtension/MJExtension.h> #import "Model.h" @implementation ViewModel - (instancetype)init { self = [super init]; if (self) { __weak typeof (self)weakSelf = self; self.command = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) { return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { [weakSelf fetchData:^(NSArray *arr) { [subscriber sendNext:arr]; [subscriber sendCompleted]; } failure:^(NSError *error) { [subscriber sendError:error]; }]; return nil; }]; }]; } return self; } - (void)fetchData:(void(^)(NSArray *arr))successBlock failure:(void(^)(NSError *error))failure{ AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; manager.responseSerializer = [AFJSONResponseSerializer serializer]; [manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSArray *arr = [Model mj_objectArrayWithKeyValuesArray:responseObject[@"subjects"]]; /** 方法一:block回调出去 if (self.dataSourceBlock) { self.dataSourceBlock(arr); } */ /** 方法二 : ReactiveCocoa */ if (successBlock) { successBlock(arr); } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { if (failure) { failure(error); } }]; } @end
- (void)viewDidLoad { [super viewDidLoad]; [self bindViewModel]; } - (void)bindViewModel { /** RAC */ @weakify(self) [self.viewModel.command.executionSignals.switchToLatest subscribeNext:^(NSArray *dataSource) { @strongify(self); self.dataSource = dataSource; NSLog(@"%@",dataSource); }]; // 返回错误 [self.viewModel.command.errors subscribeNext:^(NSError *_Nullable x) { NSLog(@"-- error : %@", x.description); }]; //执行command [self.viewModel.command execute:nil]; }
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"标题" message:@"123456" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"other", nil]; [[self rac_signalForSelector:@selector(alertView:clickedButtonAtIndex:) fromProtocol:@protocol(UIAlertViewDelegate)] subscribeNext:^(RACTuple *tuple) { NSLog(@"%@",tuple.first); NSLog(@"%@",tuple.second); NSLog(@"%@",tuple.third); }]; [alertView show];
发送通知
NSMutableArray *dataArray = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", nil]; [[NSNotificationCenter defaultCenter] postNotificationName:@"notiName" object:dataArray];
接收通知
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"notiName" object:nil] subscribeNext:^(NSNotification *notification) { NSLog(@"%@", notification.name); NSLog(@"%@", notification.object); }];
UIScrollView *scrolView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)]; scrolView.contentSize = CGSizeMake(200, 800); scrolView.backgroundColor = [UIColor redColor]; [self.view addSubview:scrolView]; [RACObserve(scrolView, contentOffset) subscribeNext:^(id x) { NSLog(@"contentOffset: %@",x); }];
[[self.button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) { NSLog(@"x:%@", x); }];
[self.tf.rac_textSignal subscribeNext:^(id x) { NSLog(@"x:%@", x); self.label.text = x; }];
[[self.tf.rac_textSignal map:^id(id value) { return [UIColor redColor]; }] subscribeNext:^(id x) { // NSLog(@"x:%@", x); }];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init]; [tap.rac_gestureSignal subscribeNext:^(id x) { NSLog(@"x:%@", x); self.redView.backgroundColor = [UIColor yellowColor]; }]; [self.redView addGestureRecognizer: tap];详情见github.