RAC即RectiveCocoa,它是github一个重量级的响应式编程框架,可以简单粗暴地处理响应事件,包括Target、通知、代理、KVO、Timer、网络异步回调等。RAC分为ReactiveObjC、ReactiveSwift、ReactiveBridge,纯OC项目用ReactiveObjC,纯swift项目用ReactiveSwift,混合开发用ReactiveBridge,下面说一说ReactiveObjC的简单应用。
一、先说一说ReactiveObjC的处理机制
二、开始在项目中导入ReactiveObjC
pod 'ReactiveObjC', '~> 3.0.0'
三、ReactiveObjC的使用
1、ReactiveObjC的基本用法
// RAC 第一种基本写法
// 创建信号
RACSignal * signal = [RACSignal createSignal:^RACDisposable * _Nullable(id _Nonnull subscriber) {
// 发送信号
[subscriber sendNext:@"I am RAC"];
return nil;
}];
// 订阅信号
[signal subscribeNext:^(id _Nullable x) {
// x:信号的內容
NSLog(@"%@",x);
}];
// RAC 第二种基本写法
[[RACSignal createSignal:^RACDisposable * _Nullable(id _Nonnull subscriber) {
[subscriber sendNext:@"I am RAC"];
return nil;
}] subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];
2、ReactiveObjC用法示例
1)、Textfield
_textField1=[[UITextField alloc]initWithFrame:CGRectMake((SCREENWIDTH-150)/2, 100, 150, 30)];
_textField1.layer.borderWidth=0.5;
_textField1.layer.borderColor=([UIColor grayColor]).CGColor; _textField1.placeholder=@"请输入账号";
[self.view addSubview:_textField1]; [[self.textField1 rac_textSignal]subscribeNext:^(NSString * _Nullable x) {
NSLog(@"%@",x);
}];
2)、Button
_btn=[[UIButton alloc]initWithFrame:CGRectMake(50, 200, SCREENWIDTH-100, 40)];
[_btn setBackgroundColor:[UIColor blueColor]];
[_btn setTitle:@"登录" forState:0];
[self.view addSubview:_btn];
[[self.btn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
NSLog(@"点击事件");
self.btn.frame=CGRectMake(50, 200, SCREENWIDTH-100, 40);
}];
3)、KVO
[[self.btn rac_valuesAndChangesForKeyPath:@"frame" options:(NSKeyValueObservingOptionNew) observer:self] subscribeNext:^(RACTwoTuple * _Nullable x) {
NSDictionary *dic=x.second;NSLog(@"frame改变了%@--%@",x.second,dic[@"kind"]);
}];
元祖RACTwoTuple
这里的RACTwoTuple是一个集合类型,其实就相当于OC的数组
例如:
//创建
RACTuple *tuple = [RACTuple tupleWithObjects:@"1", @"2", @"3", @"4", @"5", nil];
// 从别的数组中获取内容
RACTuple *tuple1 = [RACTuple tupleWithObjectsFromArray:@[@"1", @"2", @"3", @"4", @"5"]];
//利用 RAC 宏快速封装
RACTuple *tuple2 = RACTuplePack(@"1", @"2", @"3", @"4", @"5");
//遍历数组,相当于OC中的for循环数组,x是id类型,可以为任何对象
NSArray *array = @[@"1", @"2", @"3", @"4", @"5"]; [array.rac_sequence.signal subscribeNext:^(id _Nullable x) {
NSLog(@"数组内容:%@", x);
}];
4)、通知
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"XXXXXX" object:nil] subscribeNext:^(NSNotification * _Nullable x) {
NSLog(@"%@",x);
}];
5)、代理
创建自定义view PJRACView
PJRACView 的 .h中创建法法(.m中不需要实现)
PJRACView 的 .m中添加信号
-(instancetype)initWithFrame:(CGRect)frame{
self=[super initWithFrame:frame];
if (self) {
self.backgroundColor=[UIColor greenColor];
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake((SCREENWIDTH-70)/2, 65, 70, 70)];
btn.backgroundColor = [UIColor redColor];
[self addSubview:btn]; [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
//发送信号
[self send:@{@"key":@"value"}];
}];
}
return self;
}
在控制器中
PJRACView *racView = [[PJRACView alloc]initWithFrame:CGRectMake(0, 100, SCREENWIDTH, 200)];
[self.view addSubview:racView];
[[racView rac_signalForSelector:@selector(send:)] subscribeNext:^(RACTuple * _Nullable x) {
//根据方法名可传递多个参数
NSLog(@"按钮点击了%@",x.first);
}];
6)、定时器
@property (nonatomic, strong) RACDisposable *disposable;
// 定义计时器监听
self.disposable = [[RACSignal interval:1.0 onScheduler:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSDate * _Nullable x) {
NSLog(@"当前时间:%@", x);
//关闭计时器
[_disposable dispose];
}];