RAC里面80%的用法是最常用同時也是很簡單的,剩下20%的用法不常用同時也是不容易掌握的
備注:初學(xué)者很容易被RAC嚇到酬姆,特別是看到網(wǎng)上有些文章大篇幅的介紹枯燥的理論和很少用到的高級用法后。
不要被RAC嚇到奥溺,RAC并不難辞色,難的那一部分在項目中很少遇到。所以:這篇文章只介紹最常用最簡單的那 80%的用法(足夠用了~)
1浮定、 RAC - 通知 (不用手動移除通知了~)
//退出登錄的方法里發(fā)送通知:
[[NSNotificationCenterdefaultCenter]postNotificationName:@"log_out"object:nil];
// 接收通知的地方:(takeUntil:[self rac_willDeallocSignal]]這句可以保證在頁面銷毀的時候移除通知)
[[[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"login_out" object:nil] takeUntil:[self rac_willDeallocSignal]] subscribeNext:^(id x) {
NSNotification* notification = (NSNotification*)x;
// 退出登錄的時候接受到的通知
}];
2相满、RAC - UITextField輸入限制
/// 監(jiān)聽輸入金額是否合法([unowned self]可以避免swift循環(huán)引用层亿,比OC更方便)
self.codeTF.rac_textSignal().take(until: self.rac_willDeallocSignal()).subscribeNext { [unowned self] (text) in
if let text = text as? String {
if text.length >= 1 { // 輸入內(nèi)容的長度判斷
self.submitBtn.backgroundColor = UIColor.getMain()
self.submitBtn.setTitleColor(UIColor.white, for: .normal)
self.submitBtn.isUserInteractionEnabled = true
} else {
self.submitBtn.backgroundColor = UIColor.colorRGB16(value: 0xececec)
self.submitBtn.setTitleColor(UIColor.getContentSecond(), for: .normal)
self.submitBtn.isUserInteractionEnabled = false
}
}
}
3、RAC - UIButton的點擊事件
// 普通按鈕點擊
[[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
}];
//點擊事件攔截立美。(filter 可以攔截某個條件的方法)
[[[button rac_signalForControlEvents:UIControlEventTouchUpInside]filter:^BOOL(UIButton *button) {
if ([button.currentTitle isEqualToString:@"sd"]) {
return YES;
}else{
return NO;
}
}]subscribeNext:^(id x) {
NSLog(@"點擊事件");
}];
4匿又、RAC - SegmentedControl的點擊事件
[[segmentedControl rac_signalForControlEvents:UIControlEventValueChanged] subscribeNext:^(id x) {
}];
5、RAC - RACObserver
//某個類的某個屬性一發(fā)生變化就執(zhí)行建蹄。
[RACObserve(self, count) subscribeNext:^(id x) {
if ([x integerValue] == 0) {
}else if ([x integerValue] > 100){
}
}];
// 監(jiān)聽 contentOffset的值,比代理簡單的多碌更。
[RACObserve(scrollView, contentOffset) subscribeNext:^(id x) {
NSLog(@"%@",x);
}];
//返回Bool賦值給createEnabled
RAC(self, createEnabled) = [RACSignal combineLatest:@[RACObserve(self, password),RACObserve(self, passwordConfirm)] reduce:^(NSString *pwd,NSString *pwdConfirm) {
return @([pwd isEqualToString:pwdConfirm]);
}];
6、RAC - map的用法
//map 改變返回的類型給結(jié)果管道洞慎。(NSString--->UIColor)
[[self.textField.rac_textSignal map:^id(NSString *text) {
if ([text isEmptyString]) {
return [UIColor whiteColor];
}else{
return [UIColor yellowColor];
}
}]subscribeNext:^(UIColor *color) {
self.textField.backgroundColor = color;
}];
// 返回類型:UIButton --> NSString
[[[self.buttonrac_signalForControlEvents:UIControlEventTouchUpInside]map:^id(UIButton *button) {
if ([button.currentTitle isEqualToString:@"按鈕"]) {
return [NSString stringWithFormat:@"按鈕"];
}else{
return [NSString stringWithFormat:@"不是按鈕"];
}
}]subscribeNext:^(NSString *resultString) {
NSLog(@"%@",resultString);
}];
7针贬、RAC - filter 用法
//filter某個屬性滿足一定條件才執(zhí)行。
[[RACObserve(self, count) filter:^BOOL(id count) {//返回的是BOOL類型
if ([count integerValue] == 5) {
return YES;
}else{
return NO;
}
}]subscribeNext:^(id count) {//上面return YES 才走這里
NSLog(@"數(shù)量為===%@",count);
}];
8拢蛋、RAC - UI綁定模型
// RAC() 可以將Signal發(fā)出事件的值賦值給某個對象的某個屬性,其參數(shù)為對象名和屬性名
// RACObserve() 參數(shù)為對象名和屬性名蔫巩,新建一個Signal并對對象的屬性的值進(jìn)行觀察谆棱,當(dāng)值變化時Signal會發(fā)出事件
// 這里不能使用基本數(shù)據(jù)類型,RAC中傳遞的都是id類型,使用基本類型會崩潰,所以使用map
RAC(self.lb_age,text)=[RACObserve(model, age) map:^id(id value) {
return [NSString stringWithFomat:@"%@",value];
}];
// 其實上面的寫法也可以寫成:(感覺是一樣的)
[RACObserve(model, age) map:^id(id value) {
self.lb_age.text = [NSString stringWithFomat:@"%@",value];
}];
9、RAC - combineLatest的用法
//將self.button.enables 屬性 和 右邊的signal sendNext 值綁定圆仔。
RAC(self.button,enabled) = [RACSignal combineLatest:@[RACObserve(self, password),self.textField.rac_textSignal,RACObserve(self, passwordConfirm)] reduce:^(NSString *password,NSString *textString,NSString *passwordConfirm){
if ([password isEqualToString:passwordConfirm] && textString.length > 0) {
return @(YES);
}else{
return @(NO);
}
}];