?現(xiàn)在分為ReactiveObjC和ReactiveSwift
OC版本的使用
RAC 的核心是創(chuàng)建信號(hào) ->發(fā)送信號(hào)->訂閱信號(hào)
1. RACSignal 信號(hào)
/* 創(chuàng)建信號(hào) */
RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id? _Nonnull subscriber) {
? ? /*發(fā)送信號(hào) */
? ? [subscriber sendNext:@"發(fā)送信號(hào)"];
? ? return nil;
}];
/* 訂閱信號(hào) */
RACDisposable *disposable = [signal subscribeNext:^(id? _Nullable x) {
? ? NSLog(@"信號(hào)內(nèi)容:%@", x);
}];
/* 取消訂閱 */
[disposable dispose];
2. RACSubject 信號(hào)
/* 創(chuàng)建信號(hào) */
RACSubject *subject = [RACSubject subject];
/* 發(fā)送信號(hào) */
[subject sendNext:@"發(fā)送信號(hào)"];
/* 訂閱信號(hào)(通常在別的視圖控制器中訂閱,與代理的用法類似) */
[subject subscribeNext:^(id? _Nullable x) {
? ? NSLog(@"信號(hào)內(nèi)容:%@", x);
}];
3. RACTuple 元祖
/* 創(chuàng)建元祖 */
RACTuple *tuple = [RACTuple tupleWithObjects:@"1", @"2", @"3", @"4", @"5", nil];
/* 從別的數(shù)組中獲取內(nèi)容 */
RACTuple *tuple = [RACTuple tupleWithObjectsFromArray:@[@"1", @"2", @"3", @"4", @"5"]];
/* 利用 RAC 宏快速封裝 */
RACTuple *tuple = RACTuplePack(@"1", @"2", @"3", @"4", @"5");
NSLog(@"取元祖內(nèi)容:%@", tuple[0]);
NSLog(@"第一個(gè)元素:%@", [tuple first]);
NSLog(@"最后一個(gè)元素:%@", [tuple last]);
4. 便利 Array 數(shù)組和 Dictionary 字典
/* 遍歷數(shù)組 */
NSArray *array = @[@"1", @"2", @"3", @"4", @"5"];
[array.rac_sequence.signal subscribeNext:^(id? _Nullable x) {
? ? NSLog(@"數(shù)組內(nèi)容:%@", x); // x 可以是任何對(duì)象
}];
/* 遍歷字典 */
NSDictionary *dictionary = @{@"key1":@"value1", @"key2":@"value2", @"key3":@"value3"};
[dictionary.rac_sequence.signal subscribeNext:^(RACTuple * _Nullable x) {
? ? RACTupleUnpack(NSString *key, NSString *value) = x; // x是一個(gè)元祖,這個(gè)宏能夠?qū)?key 和 value 拆開
? ? NSLog(@"字典內(nèi)容:%@:%@", key, value);
}];
/* 內(nèi)容操作 */
NSArray *array = @[@"1", @"2", @"3", @"4", @"5"];
NSArray *newArray = [[array.rac_sequence map:^id _Nullable(id? _Nullable value) {
? ? NSLog(@"數(shù)組內(nèi)容:%@", value);
? ? return @"0"; //將所有內(nèi)容替換為 0
}] array];
/* 內(nèi)容快速替換 */
NSArray *array = @[@"1", @"2", @"3", @"4", @"5"];
NSArray *newArray = [[array.rac_sequence mapReplace:@"0"] array]; // 將所有內(nèi)容替換為 0
5. 監(jiān)聽 TextField 的輸入改變
/* 監(jiān)聽 TextField 的輸入(內(nèi)容改變就會(huì)調(diào)用) */
[[textField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
? ? NSLog(@"輸入框內(nèi)容:%@", x);
}];
/* 添加監(jiān)聽條件 */
[[textField.rac_textSignal filter:^BOOL(NSString * _Nullable value) {
? ? return value.length > 5; //表示輸入文字長(zhǎng)度 > 5 時(shí)才會(huì)調(diào)用下面的 block
}] subscribeNext:^(NSString * _Nullable x) {
?? ? NSLog(@"輸入框內(nèi)容:%@", x);
}];
6. 監(jiān)聽 Button 點(diǎn)擊事件
[[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
? ? NSLog(@"%@按鈕被點(diǎn)擊了", x); // x 是 button 按鈕對(duì)象
}];
7. 登錄按鈕狀態(tài)實(shí)時(shí)監(jiān)聽
RAC(submitButton, enabled) = [RACSignal combineLatest:@[phoneTextField.rac_textSignal, codeTextField.rac_textSignal] reduce:^id _Nullable(NSString * username, NSString * password){
? ? ? ? return @(username.length && password.length);
? ? }];
? ? [RACObserve(submitButton, enabled) subscribeNext:^(id x) {
? ? ? ? if ([x integerValue] == 1) {
? ? ? ? ? ? [submitButton setBackgroundColor:rgba(239, 133, 49, 1)];
? ? ? ? } else {
? ? ? ? ? ? [submitButton setBackgroundColor:TQMColor238];
? ? ? ? }
? ? }];
8. 監(jiān)聽 Notification 通知事件
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardDidShowNotification object:nil] subscribeNext:^(NSNotification * _Nullable x) {
? ? NSLog(@"%@鍵盤彈起", x); // x 是通知對(duì)象
}];
9. 代替 Delegate 代理方法
[[view rac_signalForSelector:@selector(btnClick)] subscribeNext:^(RACTuple * _Nullable x) {
? ? NSLog(@" view中的按鈕被點(diǎn)擊了");
}];
10. 代替 KVO 監(jiān)聽
[[view rac_valuesForKeyPath:@"frame" observer:self] subscribeNext:^(id? _Nullable x) {
? ? NSLog(@"屬性的改變:%@", x); // x 是監(jiān)聽屬性的改變結(jié)果
}];
[RACObserve(view, frame) subscribeNext:^(id? _Nullable x) {
? ? NSLog(@"屬性的改變:%@", x); // x 是監(jiān)聽屬性的改變結(jié)果
}];
11. 代替 NSTimer 計(jì)時(shí)器
@interface ViewController ()
@property (nonatomic, strong) RACDisposable *disposable;
@end
/* 定義計(jì)時(shí)器監(jiān)聽 */
self.disposable = [[RACSignal interval:1.0 onScheduler:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSDate * _Nullable x) {
? ? NSLog(@"當(dāng)前時(shí)間:%@", x); // x 是當(dāng)前的系統(tǒng)時(shí)間
? ? /*關(guān)閉計(jì)時(shí)器 */
? ? [_disposable dispose];
}];
信號(hào)的處理 ? ?http://www.itdecent.cn/p/aa155560bfed
1. map 映射
創(chuàng)建一個(gè)訂閱者的映射并且返回?cái)?shù)據(jù)
2.?filter 過濾,可以篩選出需要的信號(hào)變化
3.?take是獲取,skip是跳過,這兩個(gè)方法后面跟著的都是NSInteger。所以take 2就是獲取前兩個(gè)信號(hào),skip 2就是跳過前兩個(gè)。repeat是重復(fù)發(fā)送信號(hào)。
4.?delay ?延遲調(diào)用?
5. ?throttle ?節(jié)流。? ?throttle:0.5 ?只有0.5S內(nèi)信號(hào)不產(chǎn)生變化才會(huì)發(fā)送請(qǐng)求,這樣快速的輸入也不會(huì)造成多次輸出。
6.?distinctUntilChanged ?使RAC不會(huì)連續(xù)發(fā)送兩次相同的信號(hào)
7.?timeout 超時(shí) ??timeout:2 ?2秒超時(shí)會(huì)給訂閱者發(fā)送error信號(hào)
8.?ignore ?忽略信號(hào),指定一個(gè)任意類型的量(可以是字符串,數(shù)組等),當(dāng)需要發(fā)送信號(hào)時(shí)講進(jìn)行判斷,若相同則該信號(hào)會(huì)被忽略發(fā)送。