ReactiveCocoa-学习之路(一)

作者: LeoZzz | 来源:发表于2017-06-23 16:49 被阅读138次

    一、引言

    ReactiveCoca(简称为RAC),是由Github开源的一个应用于iOS和OS开发的新框架,Cocoa是苹果整套框架的简称,因此很多苹果框架喜欢以Cocoa结尾。通过RAC 我们可以接触到信的编程思想 :响应式编程。即 不用关心使用对象,只要我们订阅了信号,收到信号,响应信息。

    二、安装

    由于Swift最近很火的原因,RAC 在 2.5 版本之后 支持Swfit,纯OC版的是2.5。cocoapods导入pod 'ReactiveCocoa', '~> 2.5'

    三、简单使用

    1)通知的使用 键盘出来时收到通知 打印相关信息

    -(void)notifaction{
        [[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardWillChangeFrameNotification object:nil] subscribeNext:^(id x) {
            NSLog(@"我是键盘啊");
        }];
    }
    

    2)KVO
    当我点击viewcontroller 空白页面时,修改People属性name,vc里面监听了people的属性 修改属性时,在vc里面 的UIlabel 显示信息。

    -(void)testRAC_kvo{
        self.nameLbl = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 60, 60)];
        self.nameLbl.textColor = [UIColor redColor];
        [self.view addSubview:self.nameLbl];
        @weakify(self)
        [RACObserve(self.people, name) subscribeNext:^(id x) {
            @strongify(self)
            if (!self) {
                return ;
            }
            self.nameLbl.text = x;
        }];
    }
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        self.people.name = [NSString stringWithFormat:@"我第%d",arc4random()%100];
        
    }
    

    3)UITextfiled 使用
    第一种: 输入框输入信息 打印相关信息

    -(void)testTextFiled{
        _textfield = [[UITextField alloc]initWithFrame:CGRectMake(100, 200, 100, 40)];
        _textfield.borderStyle = UITextBorderStyleRoundedRect;
        [self.view addSubview:_textfield];
        _textfield1= [[UITextField alloc]initWithFrame:CGRectMake(200, 200, 100, 40)];
        _textfield1.borderStyle = UITextBorderStyleRoundedRect;
        [self.view addSubview:_textfield1];
        
       [[_textfield rac_textSignal]subscribeNext:^(id x) {
          NSLog(@"_textfield====>%@",x);
        }];
    }
    

    第二种 :订阅俩个输入框的信号 当俩个输入框 输入的信息 都不为空的时候 打印不能登录。

    -(void)testTextFiled{
        _textfield = [[UITextField alloc]initWithFrame:CGRectMake(100, 200, 100, 40)];
        _textfield.borderStyle = UITextBorderStyleRoundedRect;
        [self.view addSubview:_textfield];
        _textfield1= [[UITextField alloc]initWithFrame:CGRectMake(200, 200, 100, 40)];
        _textfield1.borderStyle = UITextBorderStyleRoundedRect;
        [self.view addSubview:_textfield1];
        
        // 文本框组合
        
        id _textfieldSet = @[[_textfield rac_textSignal],[_textfield1 rac_textSignal]];
        [[RACSignal combineLatest: _textfieldSet] subscribeNext:^(RACTuple * x) {
            
            NSString * one = [x first];
            NSString * two = [x second];
            if ((one.length > 0) && (two.length > 0)) {
                NSLog(@"name==%@ password==%@",one,two);
            }else{
                NSLog(@"不能登录");
            }
        }];
    }
    
    1. UIButton
      _loginBt = [[UIButton alloc]initWithFrame:CGRectMake(40, 40, 40, 40)];
        [_loginBt setTitle:@"click" forState:UIControlStateNormal];
        [_loginBt setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [[_loginBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            NSLog(@"click me");
        }];
        [self.view addSubview:_loginBt];
    
    1. Delegate
    -(void)delegateDemo{
        _textfield = [[UITextField alloc]initWithFrame:CGRectMake(100, 200, 100, 40)];
        _textfield.borderStyle = UITextBorderStyleRoundedRect;
        [self.view addSubview:_textfield];
        _textfield1= [[UITextField alloc]initWithFrame:CGRectMake(200, 200, 100, 40)];
        _textfield1.borderStyle = UITextBorderStyleRoundedRect;
        [self.view addSubview:_textfield1];
        
    
        self.proxy = [[RACDelegateProxy alloc]initWithProtocol:@protocol(UITextFieldDelegate)];
        
        [[self.proxy rac_signalForSelector:@selector(textFieldShouldReturn:)] subscribeNext:^(id x) {
            if ([_textfield hasText]) {
                [_textfield1 becomeFirstResponder];
            }
        }];
        _textfield.delegate  = (id<UITextFieldDelegate>)self.proxy;
    
    }
    
    

    四. 结语

    以上就是RAC 对 Delegate、KVO、Notifaction、UIControl的简单使用。后期会分享RAC的更高级用法。
    附上Demo

    相关文章

      网友评论

      本文标题:ReactiveCocoa-学习之路(一)

      本文链接:https://www.haomeiwen.com/subject/rmexqxtx.html