RAC(ReactiveCocoa)使用方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RAC(ReactiveCocoa)使用方法相关的知识,希望对你有一定的参考价值。
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); }];
KVO
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);
}];
Target-Action
UIButton
[[self.button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) { NSLog(@"x:%@", x); }];
UITextField
[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); }];
UIView
详情见github.UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init]; [tap.rac_gestureSignal subscribeNext:^(id x) { NSLog(@"x:%@", x); self.redView.backgroundColor = [UIColor yellowColor]; }]; [self.redView addGestureRecognizer: tap];
以上是关于RAC(ReactiveCocoa)使用方法的主要内容,如果未能解决你的问题,请参考以下文章
IOS响应式编程框架ReactiveCocoa(RAC)使用示例-备