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-学习之路(一)

    一、引言 ReactiveCoca(简称为RAC),是由Github开源的一个应用于iOS和OS开发的新框架,Co...

  • ld: framework not found Pods

    运行别人的工程,发现第三方库内容改变了(ReactiveCocoa->ReactiveObjC), 删了Podfi...

  • ReactiveCocoa-冷热信号的创建

    不了解冷热信号的看前一篇《ReactiveCocoa-冷信号和热信号的理解》RACSubject及其子类是热信号。...

  • iOS -ReactiveCocoa-基础

    1.ReactiveCocoa简介 ReactiveCocoa(简称为RAC),是由Github开源的一个应用于i...

  • iOS -ReactiveCocoa-进阶

    1.ReactiveCocoa常见操作方法介绍。 1.1 ReactiveCocoa操作须知所有的信号(RACSi...

  • Qt5学习地址

    Qt 学习之路 2(1):序(Qt 学习之路 2(1):序) Qt 学习之路 2(2):Qt 简介(Qt 学习之路...

  • ReactiveCocoa-上手其实很简单(一)

    之前写了篇RAC的方法使用,有些小伙伴说还是不会用,太抽象了。网上也有人说RAC的入门门槛高,其实我想说不要被大家...

  • springboot入门之路(二)

    springboot入门之路(二) 继springboot入门之路(一)连续的学习渐进之路。阅读springboo...

  • Java技术学习心得

    1. 学习之路,不走弯路,就是捷径 软件开发之路是充满荆棘与挑战之路,也是充满希望之路。Java学习也是如此,没有...

  • 2020-08-12

    Ceph菜鸟学习之路一

网友评论

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

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