一.体验步骤:
- 准备工作
1. 新建一个Xcode工程:RACDemo001
2. pod init
3. 在podfile文件里加上 pod ‘ReactiveObjC’
4. 终端输入: pod install,完成后重新打开程序,准备写代码
- 开始体验(编写一个类似密码输入的UI:但并不是)
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];
}
}
}];
}
效果图: