RAC初学记录
个人记录,方便查阅
场景 1. 用户登录,登录按钮需账号密码同时满足某个条件才能点击
NSArray *signals = @[self.phoneTextField.rac_textSignal, self.passwordTextField.rac_textSignal];
RAC(self.loginButton, enabled) = [RACSignal combineLatest:signals reduce:^(NSString *username, NSString *password) {
return@(username.length > 0 && password.length >= 6);
}];
场景 2. 登录密码框右边按钮,点击可查看密码,再次点击变为******
@weakify(self);
[[[self.securityButton rac_signalForControlEvents:UIControlEventTouchUpInside] map:^id(UIButton *button) {
button.selected = !button.selected;
return @(button.selected);
}] subscribeNext:^(idx) {
@strongify(self);
self.passwordTextField.secureTextEntry = ![xboolValue];
}];
场景 3. 获取验证码60S倒计时,倒计时期间按钮为不可点状态
```
- (RACSignal *)resendVerificationCode {
staticconstNSIntegertimes = 60;
RACSignal *timer = [[[RACSignal interval:1 onScheduler:[RACScheduler mainThreadScheduler]] map:^id(id value) {
return nil;
}] startWith:nil];
NSMutableArray *numbers = [[NSMutableArrayalloc] initWithCapacity:times];
for (inti = times; i >= 0; i--) {
[numbersaddObject:@(i)];
}
return [[[numbers.rac_sequence.signalzipWith:timer] map:^id(RACTuple*tuple) {
NSNumber *number = tuple.first;
NSIntegercount = number.integerValue;
if (count ==0) {
returnRACTuplePack(@"获取验证码",@YES);
} else {
NSString *title = [NSStringstringWithFormat:@"重新获取(%@)", number];
return RACTuplePack(title,@NO);
}
}] takeUntil:self.rac_willDeallocSignal];
}
- (void)setupCodeButton {
@weakify(self);
[[[[[[self.codeButton rac_signalForControlEvents:UIControlEventTouchUpInside] map:^id(id value) {
@strongify(self);
if([self.bankIDTextField.textisEmptyString]) {
[SVProgressHUD showErrorWithStatus:@"银行卡号不能为空!"];
return nil;
} else {
// if (![self.bankIDTextField.text isValidBankCardNumber]) {
// [SVProgressHUD showErrorWithStatus:@"请输入正确的银行卡号!"];
// return nil;
// }
}
if([self.mobileTextField.textisEmptyString]) {
[SVProgressHUD showErrorWithStatus:@"手机号码不能为空!"];
return nil;
} else {
if (![self.mobileTextField.textisValidMobileNumber]) {
[SVProgressHUD showErrorWithStatus:@"请输入有效的手机号码!"];
return nil;
}
}
[selfrequestCode];
return [self resendVerificationCode];
}] startWith:nil] switchToLatest] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(RACTuple *tuple) {
@strongify(self);
dispatch_async(dispatch_get_main_queue(), ^{
[self.codeButton setTitle:tuple.first forState:UIControlStateNormal];
[self.codeButtonsetEnabled:[tuple.secondboolValue]];
});
}];
}
```
不喜勿喷,个人记录方便翻阅,后续如有用到更多场景功能会添加