RAC简单使用

简介

RAC-ReactiveCocoa 响应式编程,它可以用来代替系统的一些事件(点击,通知kvo,定时器等等)。用它可以更方便的处理系统事件。往往MVVM中用它处理kvo。其实mvc或者mvp或者mvvm等都可以用rac来处理事件,只不过mvvm用rac来代替kvo更方便而已。

示例代码

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self textFieldCombineLastet];
}

- (void)btnClick{
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    btn.backgroundColor = [UIColor redColor];
    [self.view addSubview:btn];
    
    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        NSLog(@"btn = %@",x);
    }];
    
    //防止由于cell重用导致按钮被触发多次
    [[[btn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:cell.rac_prepareForReuseSignal] subscribeNext:^(__kindof UIControl * _Nullable x) {
            NSLog(@"btn = %@",x);
    }];
}

- (void)kvoEvent{
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    btn.backgroundColor = [UIColor redColor];
    [self.view addSubview:btn];
    
    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        x.frame = CGRectMake(100, 100, 100, 100);
    }];
    
    //只是监听新值变化
//    [RACObserve(btn, frame) subscribeNext:^(id  _Nullable x) {
//        NSLog(@"x = %@",x);
//    }];
    
    [[btn rac_valuesAndChangesForKeyPath:@"frame" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew observer:self] subscribeNext:^(RACTwoTuple * _Nullable x) {
        NSLog(@"x = %@",x);
    }];
}

- (void)protocolEvent{
    ViewTest *view = [[ViewTest alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    view.backgroundColor = [UIColor redColor];
    [self.view addSubview:view];
    
    //单值
    [view.btnClickSingle subscribeNext:^(id  _Nullable x) {
        NSLog(@"x = %@",x);
    }];
    
    //多值
    [[view rac_signalForSelector:@selector(sendText:text2:)] subscribeNext:^(RACTuple * _Nullable x) {
        NSLog(@"x = %@",x);
    }];
}

- (void)notificationEvent{
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 50.0)];
    textField.backgroundColor = [UIColor grayColor];
    [self.view addSubview:textField];
    
//    [[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardDidShowNotification object:nil] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSNotification * _Nullable x) {
//        NSLog(@"x = %@",x);
//    }];
    
//    [textField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
//        NSLog(@"x = %@",x);
//    }];
    
    [[textField.rac_textSignal filter:^BOOL(NSString * _Nullable value) {
        return value.length > 5;
    }]subscribeNext:^(NSString * _Nullable x) {
        NSLog(@"长度大于5");
    }];
}

- (void)timerEvent{
    @weakify(self);
   self.disposable = [[RACSignal interval:1.0 onScheduler:[RACScheduler scheduler]] subscribeNext:^(NSDate * _Nullable x) {
       @strongify(self);
        NSLog(@"x = %@",x);
       [self.disposable dispose];
    }];
}

- (void)maneySingle{
    RACSignal *signal1 = [RACSignal createSignal:^RACDisposable * _Nullable(id  _Nonnull subscriber) {
        [subscriber sendNext:@"1"];
        return nil;
    }];
    
    RACSignal *signal2 = [RACSignal createSignal:^RACDisposable * _Nullable(id  _Nonnull subscriber) {
        [subscriber sendNext:@"2"];
        return nil;
    }];
    
    [self rac_liftSelector:@selector(updateData:str2:) withSignalsFromArray:@[signal1,signal2]];
}

- (void)updateData:(NSString *)str1 str2:(NSString *)str2{
    NSLog(@"str1 = %@, str2 = %@",str1,str2);
}

- (void)textFieldCombineLastet{
    UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 40.0)];
    textField1.backgroundColor = [UIColor grayColor];
    [self.view addSubview:textField1];
    
    UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(100, 150, 100, 40.0)];
    textField2.backgroundColor = [UIColor grayColor];
    [self.view addSubview:textField2];
    
    UIButton *btnLogin = [[UIButton alloc] initWithFrame:CGRectMake(100, 200, 80, 40.0)];
    btnLogin.backgroundColor = [UIColor grayColor];
    [self.view addSubview:btnLogin];
    
    RAC(btnLogin,backgroundColor) = [RACSignal combineLatest:@[textField1.rac_textSignal,textField2.rac_textSignal] reduce:^id _Nonnull(NSString *strUserName, NSString *strPassword){
        return (strUserName.length > 0 && strPassword.length > 0) ? [UIColor orangeColor] : [UIColor grayColor];
    }];
}
- (void)tapGestureRecognizer{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    view.backgroundColor = [UIColor redColor];
    [self.view addSubview:view];
    
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] init];
    [view addGestureRecognizer:tapGR];
    [[tapGR rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        NSLog(@"x = %@",x);
    }];
}

- (void)btnDataBind{
    @weakify(self);
    //vm对象的arrClassifyFirst属性的值一变化(skip:1跳过初始值赋值,也就是第一次赋值,往往要这样写)就进入block
    [[RACObserve(self.vm, arrClassifyFirst) skip:1] subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self.tableView reloadData];
        NSLog(@"btnDataBind");
    }];
}

- (void)addCellAuction:(UITableViewCell *)cell{
    @weakify(self);
    @weakify(cell);
    //cell上的btnLeft被点击(takeUntil:cell.rac_prepareForReuseSignal 解决重用多次添加事件问题),就进入回调block
    [[[cell.btnLeft rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:cell.rac_prepareForReuseSignal] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        @strongify(cell);
    }];
}

你可能感兴趣的:(iOS)