刘宏立博客

RAC-响应式编程之初体验

一.体验步骤:

  1. 准备工作
1
2
3
4
1. 新建一个Xcode工程:RACDemo001
2. pod init
3. 在podfile文件里加上 pod ‘ReactiveObjC’
4. 终端输入: pod install,完成后重新打开程序,准备写代码

podfile文件.png

  1. 开始体验(编写一个类似密码输入的UI:但并不是)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    1. 首先导入头文件: #import <ReactiveObjC.h>
    2. 代码正文:
    - (void)viewDidLoad {
    [super viewDidLoad];
    [self firstRAC];
    }
    - (void)firstRAC {
    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(self.view.center.x - 100, 100, 200, 40)];
    [self.view addSubview:textField];
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.text = @"";
    textField.placeholder = @"请输入密码";
    textField.secureTextEntry = YES;
    NSMutableArray <UILabel *>*arrayM = [NSMutableArray array];
    for (NSInteger i = 0; i<=3; i++) {
    UILabel *label = [[UILabel alloc]init];
    label.tag = i;
    label.backgroundColor = [UIColor clearColor];
    [self.view addSubview:label];
    label.frame = CGRectMake(self.view.center.x + i*51 - 100, 150, 47, 10);
    [arrayM addObject:label];
    }
    UILabel *tipLabel = [[UILabel alloc]init];
    [self.view addSubview:tipLabel];
    tipLabel.frame = CGRectMake(self.view.center.x - 100, 170, 200, 10);
    tipLabel.font = [UIFont systemFontOfSize:11];
    tipLabel.textColor = [UIColor greenColor];
    tipLabel.text = @"";
    ///核心代码
    [[textField rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(id x){
    NSLog(@"textField.text========%@", textField.text);
    NSInteger j = textField.text.length;
    NSInteger k = 0;
    if (j <= 0) {
    tipLabel.text = @"";
    k=0;
    }
    else if (j>0 && j<=6) {
    k = 1;
    }
    else if (j>6 && j<=9) {
    k=2;
    }
    else if (j>9 && j<=12) {
    k=3;
    }
    else if (j>12 && j<=15) {
    k=4;
    }
    else {
    return ;
    }
    for (NSInteger i = 0; i < 4; i++) {
    if (i <= k - 1) {
    arrayM[i].backgroundColor = [UIColor magentaColor];
    if (k == 1) {
    tipLabel.text = @"密码太弱";
    tipLabel.textColor = [UIColor grayColor];
    }
    else if (k==2) {
    tipLabel.text = @"密码一般";
    tipLabel.textColor = [UIColor orangeColor];
    }
    else if (k==3) {
    tipLabel.text = @"密码较强";
    tipLabel.textColor = [UIColor redColor];
    }
    else if (k==4) {
    tipLabel.text = @"密码非常强";
    tipLabel.textColor = [UIColor purpleColor];
    }
    }
    else {
    arrayM[i].backgroundColor = [UIColor clearColor];
    }
    }
    }];
    }

——–

效果图:

输入密码前.png

输入1到6位密码.png

输入7到9位密码.png

输入10到12位密码.png

输入13到15位密码.png