iOS接下来要研究的知识点

RAC(ReactiveCocoa)的简单使用

2018-12-26  本文已影响0人  辛乐

参考文章:https://www.jianshu.com/p/aa155560bfed,这里比我写的详细多了
下面代码只是个人书写Demo,方便自己使用

特别注意这里使用大量block, 注意使用@weakify(self)防止循环引用

前提代码准备

UILabel *label = [[UILabel alloc] init];
    label.tag = 100;
    label.frame = CGRectMake(15, 130, 200, 40);
    label.backgroundColor = [UIColor yellowColor];
    label.textColor = [UIColor redColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:label];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(15, 190, 200, 40);
    [btn setTitle:@"清空数据" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:btn];
    
    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(15, 240, 200, 40)];
    tf.placeholder = @"文本输入框";
    tf.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:tf];

//1.0 rac_signalForControlEvents 事件类绑定

    [[tf rac_signalForControlEvents:(UIControlEventEditingChanged)] subscribeNext:^(id x) {
        NSLog(@"~~~%@~~~",x);
        UITextField *tf = x;
        label.text = tf.text;
    }];

1.1 throttle 节流,可以用于防止连续点击(0.5s),效果最合适,当前次的操作也会有节流的效果
还可以用于textfield的实时搜索(需要请求接口)的地方

    [[[btn rac_signalForControlEvents:(UIControlEventTouchUpInside)] throttle:0.5] subscribeNext:^(id x) {
        tf.text = nil;
        //2.0 rac_textSignal 实时监控文本信息
        [[tf rac_textSignal] subscribeNext:^(id x) {
            label.text = x;
        }];
    }];

//3.0 rac_gestureSignal 手势的信号

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    [[tap rac_gestureSignal] subscribeNext:^(id x) {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"RAC" message:@"alertView测试" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
//        /**没啥效果*/
//        [[alert rac_signalForSelector:@selector(alertView:clickedButtonAtIndex:) fromProtocol:@protocol(UIAlertViewDelegate)] subscribeNext:^(RACTuple *tuple) {
//            NSLog(@"~~~%@~~~",tuple);
//        }];
        //4.0 rac_buttonClickedSignal 可以触发点击事件
        [[alert rac_buttonClickedSignal] subscribeNext:^(id x) {
            NSLog(@"~~~%@~~~",x);
            if ([NSString stringWithFormat:@"%@",x].integerValue == 1) {
                XLViewController *xlVC = [[XLViewController alloc] init];
                [self presentViewController:xlVC animated:YES completion:nil];
            }
        }];
        [alert show];
    }];
    label.userInteractionEnabled = YES;
    [label addGestureRecognizer:tap];

通知中心的使用

    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"xlVCDismiss" object:nil] subscribeNext:^(id x) {
        label.text = ((NSNotification *)x).object;
    }];

//6.0 RACObserve KVO使用

    [RACObserve(label, text) subscribeNext:^(id x) {
        NSLog(@"~~~监听:%@~~~",x);
    }];

//7.0 map 映射,可以映射到需要监听的任意值

    [[[tf rac_textSignal] map:^id(id value) {
        return @(tf.text.length);
    }] subscribeNext:^(id x) {
        NSLog(@"~~~此处返回的是map映射的值具体结果:%@~~~",x);
    }];

filter 过滤,可以根据具体的需求过滤,比如长度大于等于3,并且大于200

    [[[tf rac_textSignal] filter:^BOOL(id value) {
        return ([value length] >= 3 && [value floatValue] > 200);
    }] subscribeNext:^(id x) {
        NSLog(@"~~~此处返回的是filter过滤出结果:%@~~~",x);
    }];

//9.0weakify()防止循环引用造成的内存泄露

    @weakify(self);
    [[self.btn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(id x) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"xlVCDismiss" object:@"通知参数..."];
        [self_weak_ dismissViewControllerAnimated:YES completion:nil];
    }];

或者  @weakify(self) (@strongify(self))成对出现~~~

@weakify(self);
    [[self.btn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(id x) {
        @strongify(self);
        [[NSNotificationCenter defaultCenter] postNotificationName:@"xlVCDismiss" object:@"通知参数..."];
        [self dismissViewControllerAnimated:YES completion:nil];
    }];

上一篇下一篇

猜你喜欢

热点阅读