響應式編程

1.此處使用https://github.com/ReactiveCocoa/ReactiveObjC.git

platform :ios,'8.0'

target? 'RacDemo'? do?

pod? 'ReactiveObjC'

end

2.使用

1)引入頭文件

#import <ReactiveObjC.h>

2)文本輸入框監(jiān)聽

UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, 36)];

? ? field.text = @"placeholder";

? ? field.backgroundColor = [UIColor cyanColor];

? ? [self.viewaddSubview:field];

? ? [field.rac_textSignal subscribeNext:^(NSString * _Nullable x) {

? ? ? ? NSLog(@"%@",x);// 第一次返回為已經(jīng)賦值的文本

? ? }];

3)多條件判斷

RACSignal*signalFirst = [field.rac_textSignalmap:^id_Nullable(NSString*_Nullablevalue) {

? ? ? ? if(value.length>5&& value.length<10) {

? ? ? ? ? ? return@(YES);

? ? ? ? }

? ? ? ? return@(NO);

? ? }];


? ? RACSignal*signalSecond = [fieldSecond.rac_textSignalmap:^id_Nullable(NSString*_Nullablevalue) {

? ? ? ? if(value.length>5&& value.length<10) {

? ? ? ? ? ? return@(YES);

? ? ? ? }

? ? ? ? return@(NO);

? ? }];

? ? RAC(btn,enabled) = [RACSignalcombineLatest:@[signalFirst, signalSecond]reduce:^(NSNumber*valueFirst,NSNumber*valueSecond){

? ? ? ? return@(valueFirst.boolValue&& valueSecond.boolValue);

? ? }];

4)綁定label和textFiled

_label= [[UILabelalloc]initWithFrame:CGRectMake(0,151+16+(36+16)*2,self.view.bounds.size.width,36)];

? ? [self.view addSubview:_label];

? ? RAC(_label,text) = self.textField.rac_textSignal;

5)kvo

? ? self.textString = @"123";

? ? self.textString = @"456";

? ? self.textString = @"ddd";// 這里開始返回

? ? [RACObserve(self,textString)subscribeNext: ^(NSString*newString){

? ? ? ? NSLog(@"newString = %@", newString);

? ? }];

? ? self.textString = @"ads";

? ? self.textString = @"adsmmmm";

6)nofication

?? //NSNotification 添加信號

? ? [[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"MYNOTIFICATION" object:nil] subscribeNext:^(NSNotification *noti) {

? ? ? ? NSLog(@"*****? Notification Received? %@ *****",noti.userInfo[@"data"]);

? ? }];


? ? NSNotification *noti1 = [[NSNotification alloc]initWithName:@"MYNOTIFICATION" object:nil userInfo:@{@"data":[NSString stringWithFormat:@"%d",arc4random()%100]}];

? ? [[NSNotificationCenter defaultCenter] postNotification:noti1];



? ? NSNotification *noti2 = [[NSNotification alloc]initWithName:@"MYNOTIFICATION" object:nil userInfo:@{@"data":[NSString stringWithFormat:@"%d",arc4random()%100]}];

? ? [[NSNotificationCenter defaultCenter] postNotification:noti2];

7)target_action

? ? UIButton *bu = [UIButton buttonWithType:UIButtonTypeCustom];

? ? bu.frame=CGRectMake(0,151+16+36+16,self.view.bounds.size.width,36);

? ? bu.backgroundColor = [UIColor orangeColor];

? ? [self.viewaddSubview:bu];

? ? [[burac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {

? ? ? ? NSLog(@"*****? 響應RAC button的點擊? *****");

? ? }];


? ? //手勢

? ? self.view.userInteractionEnabled = YES;

? ? UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] init];

? ? [[taprac_gestureSignal] subscribeNext:^(UITapGestureRecognizer * tap) {

? ? ? ? NSLog(@"*****? 響應單擊手勢? *****");

? ? }];

? ? [self.view addGestureRecognizer:tap];

8)延遲

? ? [[RACScheduler mainThreadScheduler] afterDelay:2 schedule:^{

? ? ? ? NSLog(@"*****? first 延時rac寫法? *****");

? ? }];// 單次


? ? [[RACSignal interval:1 onScheduler:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSDate * date) {

? ? ? ? NSLog(@"***** second 延時rac寫法? *****");

? ? }];// 重復

9)組

- (void)group {

? ? // 處理多個請求,都返回結果的時候,統(tǒng)一做處理.

? ? RACSignal *request1 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {

? ? ? ? // 發(fā)送請求1

? ? ? ? [subscribersendNext:@"發(fā)送請求1"];

? ? ? ? returnnil;

? ? }];


? ? RACSignal *request2 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {

? ? ? ? // 發(fā)送請求2

//? ? ? ? [subscriber sendNext:@"發(fā)送請求2"];

? ? ? ? returnnil;

? ? }];


? ? // 使用注意:幾個信號,參數(shù)一的方法就幾個參數(shù),每個參數(shù)對應信號發(fā)出的數(shù)據(jù)。

? ? [selfrac_liftSelector:@selector(totalFuctonR1:R2:)withSignalsFromArray:@[request1,request2]];

}

-(void)totalFuctonR1:(id)data1 R2:(id)data2{

? ? NSLog(@"總方法觸發(fā):data1 = %@? ? -----? data2 = %@",data1,data2);

}

10)元組

? ? // 把參數(shù)中的數(shù)據(jù)包裝成元組

? ? RACTuple *tuple = RACTuplePack(@"xmg",@20,@"m",@(999),@[@"a"],@{@"key":@"value"});


? ? RACTupleUnpack(NSString *name,NSNumber *age,NSString *sex,NSNumber *price,NSArray *arr,NSDictionary *dic) = tuple;

? ? NSLog(@"name:%@? age:%@? sex:%@? price:%@ arr:%@? dic:%@",name,age,sex,price,arr,dic);

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容