ReactiveCocoa的简单使用

推荐比较好的一篇文章:https://www.jianshu.com/p/87ef6720a096

1、导入

如果项目是纯 OC 项目,你需要使用的是 ReactiveObjC

    use_frameworks!
    target 'Target名称' do
    pod 'ReactiveObjC'
    end

2、使用

a、代替Delegate
    RedView.h
    @interface RedView : UIView

    @end

    RedView.m
    #import "RedView.h"
    @interface RedView()
    @property(nonatomic ,strong) UIButton *redBtn;
    @end

    @implementation RedView

    - (instancetype)init
    {
        if (self = [super init]) {
            _redBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            _redBtn.backgroundColor = [UIColor orangeColor];
            [self addSubview:_redBtn];
            [_redBtn addTarget:self action:@selector(redBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        return self;
    }
    - (void)layoutSubviews
    {
        [super layoutSubviews];
        self.redBtn.frame = self.bounds;
    }
    - (void)redBtnClick:(UIButton *)btn
    {
        NSLog(@"%s 点击了RedView的RedBtn", __FUNCTION__);
    }
    @end

    ViewController.m
    #pragma mark - RAC代替Delegate
      - (void)racReplaceDelegate
    {
        _redView = [[RedView alloc]init];
        _redView.frame = CGRectMake(100, 100, 100, 100);
        _redView.center = self.view.center;
        _redView.backgroundColor = [UIColor redColor];
        [self.view addSubview:_redView];

        // RAC:判断下一个方法有没有调用,如果调用了就会自动发送一个信号给你
        // 监听_redView有没有调用redBtnClick:,如果调用了就会转换成信号
        [[_redView rac_signalForSelector:@selector(redBtnClick:)] subscribeNext:^(id  _Nullable x) { 
              NSLog(@"%s 知道了红色View的RedBtnClick,可以做事情了", __FUNCTION__);
              NSLog(@"%s x = %@", __FUNCTION__, x);
        }];
    }
b、代替KVO
    @property(nonatomic ,assign) int num;
    #pragma mark - RAC代替KVO
    - (void)racReplaceKVO
    {
        // 监听哪个对象的属性改变
        // 方法调用者:就是被监听的对象
        // KeyPath:监听的属性
        // 把监听到内容转换成信号
        [[self rac_valuesForKeyPath:@"num" observer:self] subscribeNext:^(id  _Nullable x) {
            // block:只要属性改变就会调用,并且把改变的值传递给你
            NSLog(@"%s x = %@", __FUNCTION__, x);
        }];
    }
    /// 改变num
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        self.num++;
    }
c、 监听事件
    #pragma mark - RAC监听事件
    - (void)racObserveEvent
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(100, 100, 100, 100);
        btn.center = self.view.center;
        btn.backgroundColor = [UIColor purpleColor];
        [self.view addSubview:btn];

        UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn2.frame = CGRectMake(100, 100, 100, 100);
        btn2.backgroundColor = [UIColor orangeColor];
        [self.view addSubview:btn2];

        [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            NSLog(@"%s RAC监听到了btn---UIControlEventTouchUpInside事件", __FUNCTION__);
            NSLog(@"%s x = %@", __FUNCTION__, x);
        }];
        [[btn2 rac_signalForControlEvents:UIControlEventTouchDown] subscribeNext:^(__kindof UIControl * _Nullable x) {
            NSLog(@"%s RAC监听到了btn2---UIControlEventTouchDown事件", __FUNCTION__);
            NSLog(@"%s x = %@", __FUNCTION__, x);
        }];
    }
d、监听文本框文字改变
    @property(nonatomic ,strong) UITextField *textField;
    @property(nonatomic ,strong) UITextView *textView;
    #pragma mark - 监听文本框文字改变
    - (void)racObserveTextField
    {
        _textField = [[UITextField alloc]init];
        _textField.frame = CGRectMake(50, 200, 300, 50);
        _textField.backgroundColor = [UIColor lightGrayColor];
        [self.view addSubview:_textField];

        [[_textField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
            NSLog(@"%s 监听到textField文字改变", __FUNCTION__);    //相当于发出了 UITextFieldTextDidChangeNotification
            NSLog(@"%s x = %@", __FUNCTION__, x);
        }];
    }
    - (void)racObserveTextView
    {
        _textView = [[UITextView alloc]init];
        _textView.frame = CGRectMake(50, 200, 300, 50);
        _textView.backgroundColor = [UIColor lightGrayColor];
        [self.view addSubview:_textView];

        [[_textView rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
            NSLog(@"%s 监听到textView文字改变", __FUNCTION__);    //相当于发出了  UITextViewTextDidChangeNotification
            NSLog(@"%s x = %@", __FUNCTION__, x);
        }];
    }
e、代替通知
    #pragma mark - RAC代替通知
    - (void)racReplaceNotification
    {
        [[[NSNotificationCenter defaultCenter] rac_addObserverForName:UITextViewTextDidChangeNotification object:nil] subscribeNext:^(NSNotification * _Nullable x) {
            NSLog(@"%s 接收到通知,x = %@", __FUNCTION__, x);
        }];
    }
f、处理多个请求,都返回结果,统一做处理
    - (void)testRACSignal
    {
         // 创建信号A
        RACSignal *signalA = [RACSignal createSignal:^RACDisposable * _Nullable(id  _Nonnull subscriber) {
           // 处理信号
            NSLog(@"%s 创建信号A", __FUNCTION__);
            // 发送数据
            [subscriber sendNext:@"热门商品"];
            return nil;
        }];
        // 创建信号B
        RACSignal *signalB = [RACSignal createSignal:^RACDisposable * _Nullable(id  _Nonnull subscriber) {
           // 处理信号
            NSLog(@"%s 创建信号", __FUNCTION__);
            // 发送数据
            [subscriber sendNext:@"最新商品"];
    
            return nil;
        }];

        // RAC:就可以判断两个信号有没有都发出内容
        // SignalsFromArray:监听哪些信号的发出
        // 当signals数组中的所有信号都发送sendNext就会触发方法调用者(self)的selector
        // 注意:selector方法的参数不能乱写,有几个信号就对应几个参数
        // 不需要主动订阅signalA,signalB,方法内部会自动订阅
        [self rac_liftSelector:@selector(updateUIWithHot: new:) withSignalsFromArray:@[signalA,signalB]];
    }
    - (void)updateUIWithHot:(NSString *)hot new:(NSString *)new
    {
        NSLog(@"%s hot = %@ , new = %@", __FUNCTION__, hot ,new);
    }

Demo链接:https://github.com/iMaBiao/RACDemo.git

你可能感兴趣的:(ReactiveCocoa的简单使用)