?現(xiàn)在分為ReactiveObjC和ReactiveSwift
OC版本的使用
RAC 的核心是創(chuàng)建信號 ->發(fā)送信號->訂閱信號
1. RACSignal 信號
/* 創(chuàng)建信號 */
RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id? _Nonnull subscriber) {
? ? /*發(fā)送信號 */
? ? [subscriber sendNext:@"發(fā)送信號"];
? ? return nil;
}];
/* 訂閱信號 */
RACDisposable *disposable = [signal subscribeNext:^(id? _Nullable x) {
? ? NSLog(@"信號內(nèi)容:%@", x);
}];
/* 取消訂閱 */
[disposable dispose];
2. RACSubject 信號
/* 創(chuàng)建信號 */
RACSubject *subject = [RACSubject subject];
/* 發(fā)送信號 */
[subject sendNext:@"發(fā)送信號"];
/* 訂閱信號(通常在別的視圖控制器中訂閱乡括,與代理的用法類似) */
[subject subscribeNext:^(id? _Nullable x) {
? ? NSLog(@"信號內(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(@"第一個元素:%@", [tuple first]);
NSLog(@"最后一個元素:%@", [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 可以是任何對象
}];
/* 遍歷字典 */
NSDictionary *dictionary = @{@"key1":@"value1", @"key2":@"value2", @"key3":@"value3"};
[dictionary.rac_sequence.signal subscribeNext:^(RACTuple * _Nullable x) {
? ? RACTupleUnpack(NSString *key, NSString *value) = x; // x是一個元祖樊零,這個宏能夠?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)容改變就會調(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; //表示輸入文字長度 > 5 時才會調(diào)用下面的 block
}] subscribeNext:^(NSString * _Nullable x) {
?? ? NSLog(@"輸入框內(nèi)容:%@", x);
}];
6. 監(jiān)聽 Button 點擊事件
[[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
? ? NSLog(@"%@按鈕被點擊了", x); // x 是 button 按鈕對象
}];
7. 登錄按鈕狀態(tài)實時監(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 是通知對象
}];
9. 代替 Delegate 代理方法
[[view rac_signalForSelector:@selector(btnClick)] subscribeNext:^(RACTuple * _Nullable x) {
? ? NSLog(@" view中的按鈕被點擊了");
}];
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 計時器
@interface ViewController ()
@property (nonatomic, strong) RACDisposable *disposable;
@end
/* 定義計時器監(jiān)聽 */
self.disposable = [[RACSignal interval:1.0 onScheduler:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSDate * _Nullable x) {
? ? NSLog(@"當前時間:%@", x); // x 是當前的系統(tǒng)時間
? ? /*關閉計時器 */
? ? [_disposable dispose];
}];
信號的處理 ? ?http://www.reibang.com/p/aa155560bfed
1. map 映射
創(chuàng)建一個訂閱者的映射并且返回數(shù)據(jù)
2.?filter 過濾,可以篩選出需要的信號變化
3.?take是獲取,skip是跳過绘迁,這兩個方法后面跟著的都是NSInteger。所以take 2就是獲取前兩個信號喂饥,skip 2就是跳過前兩個亲轨。repeat是重復發(fā)送信號。
4.?delay ?延遲調(diào)用?
5. ?throttle ?節(jié)流孩锡。? ?throttle:0.5 ?只有0.5S內(nèi)信號不產(chǎn)生變化才會發(fā)送請求酷宵,這樣快速的輸入也不會造成多次輸出。
6.?distinctUntilChanged ?使RAC不會連續(xù)發(fā)送兩次相同的信號
7.?timeout 超時 ??timeout:2 ?2秒超時會給訂閱者發(fā)送error信號
8.?ignore ?忽略信號躬窜,指定一個任意類型的量(可以是字符串浇垦,數(shù)組等),當需要發(fā)送信號時講進行判斷荣挨,若相同則該信號會被忽略發(fā)送男韧。