ReactiveCocoa实践

1.按钮addTarget

    [[self.aDepositBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        //点击处理
    }];


2.View加tap事件

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    @weakify(self);
    [[tap rac_gestureSignal] subscribeNext:^(id x) {
        @strongify(self);
    }];
    [infoView addGestureRecognizer:tap];

 

 3.KVO监听对象的某个属性

    [RACObserve(self.mobileTextField, text) subscribeNext:^(id  _Nullable x) {
        NSString * text = (NSString *)x;
        if (text.length) {
            //将提交按钮置为蓝色
        } else {
            //将提交按钮置为灰色
        }
    }];

 

4.通知传值

    [[NSNotificationCenter defaultCenter] postNotificationName:@"RACNotification" object:@[@"1",@"2"]];

 

    [[[NSNotificationCenter defaultCenter]rac_addObserverForName:@"RACNotification" object:nil]subscribeNext:^(id x) {
        NSLog(@"%@",x);
    }];

 

5.UITextField输入结束等类似的代理方法 

    [[self.textField rac_signalForControlEvents:UIControlEventEditingDidEnd] subscribeNext:^(id x){
        //x是textField对象
        NSLog(@"%@",x);
    }];

 

6.监听视图Frame更新UI

因为KVO监听旧值和新值,所以会调用两次

    [RACObserve(_mAreaView, frame) subscribeNext:^(id  _Nullable x) {
        CGRect rect = [x CGRectValue];
        //更新UI
    }];

 

7.定时器

    //RAC 定时器
    [[RACSignal interval:1 onScheduler:[RACScheduler scheduler]] subscribeNext:^(NSDate * _Nullable x) {
        NSLog(@"%@",x);
    }];

 

8.替代代理、替代block

A类push到B类,B给A回传数据

B的.h类

@property(nonatomic,strong)RACSubject *subject;

A类的push到B类的地方,实现监听

    B *b = [[B alloc]init];
    RACSubject *subject = [RACSubject subject];
    // 接受信号
    [subject subscribeNext:^(id  _Nullable x) {
        NSLog(@"点击了事件");
    }];
    b.subject = subject;
    [self.navigationController pushViewController:b animated:YES];

B类开始传值

    [self.subject sendNext:@"传的数据"];

 

9.MVVM中使用RACCommand做网络请求

https://www.jianshu.com/p/1a9c3500f543   

 

 

posted @ 2019-08-07 20:45  黄增松  阅读(229)  评论(0编辑  收藏  举报