一.體驗(yàn)步驟:
- 準(zhǔn)備工作
1. 新建一個(gè)Xcode工程:RACDemo001
2. pod init
3. 在podfile文件里加上 pod ‘ReactiveObjC’
4. 終端輸入: pod install,完成后重新打開程序,準(zhǔn)備寫代碼
- 開始體驗(yàn)(編寫一個(gè)類似密碼輸入的UI:但并不是)
1. 首先導(dǎo)入頭文件: #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 = @"請(qǐng)輸入密碼";
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 = @"密碼較強(qiáng)";
tipLabel.textColor = [UIColor redColor];
}
else if (k==4) {
tipLabel.text = @"密碼非常強(qiáng)";
tipLabel.textColor = [UIColor purpleColor];
}
}
else {
arrayM[i].backgroundColor = [UIColor clearColor];
}
}
}];
}
效果圖: