ReactiveCocoa開發(fā)中常見用法。
代替代理:
rac_signalForSelector:用于替代代理。
代替KVO :
rac_valuesAndChangesForKeyPath:用于監(jiān)聽某個對象的屬性改變。
監(jiān)聽事件:
rac_signalForControlEvents:用于監(jiān)聽某個事件。
代替通知:
rac_addObserverForName:用于監(jiān)聽某個通知。
監(jiān)聽文本框文字改變:
rac_textSignal:只要文本框發(fā)出改變就會發(fā)出這個信號。
處理當(dāng)界面有多次請求時,需要都獲取到數(shù)據(jù)時,才能展示界面
rac_liftSelector:withSignalsFromArray:Signals:當(dāng)傳入的Signals(信號數(shù)組),每一個signal都至少sendNext過一次,就會去觸發(fā)第一個selector參數(shù)的方法。
使用注意:幾個信號,參數(shù)一的方法就幾個參數(shù),每個參數(shù)對應(yīng)信號發(fā)出的數(shù)據(jù)。
溫馨提示:Demo用到的第三方
導(dǎo)入的第三方框架
pod 'ReactiveCocoa', '~> 2.5'
pod 'Masonry', '~> 0.6.4'
pod 'LxDBAnything', '1.1.0'
代碼:
@weakify(Obj)和@strongify(Obj),一般兩個都是配套使用,在主頭文件(ReactiveCocoa.h)中并沒有導(dǎo)入,需要自己手動導(dǎo)入,RACEXTScope.h才可以使用。但是每次導(dǎo)入都非常麻煩,只需要在主頭文件自己導(dǎo)入就好了。
/*************************************基礎(chǔ)部分**************************************************/
#pragma mark --textField屬性變化
-(void)textFieldChange
{
//初始化一個textField控件
UITextField *textField = ({
UITextField *textField = [[UITextField alloc] init];
textField.backgroundColor = [UIColor cyanColor];
textField ;
});
textField.delegate = self ;
[self.view addSubview:textField];
@weakify(self);
[textField mas_makeConstraints:^(MASConstraintMaker *make) {
@strongify(self);
make.size.mas_equalTo(CGSizeMake(180, 40));
make.center.equalTo(self.view);
}];
/*****************監(jiān)聽textField的屬性變化情況*******************/
//RAC內(nèi)部封裝好的類
//默認(rèn)執(zhí)行1次,所以會有打印,原因:請查看RAC底層bind,hook的思想
//監(jiān)聽文本框的文字改變
[textField.rac_textSignal subscribeNext:^(id x) {
LxDBAnyVar(x);
}];
}
#pragma mark -- Button用法 監(jiān)聽事件
-(void)textBtn
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:@"點(diǎn)擊" forState:UIControlStateNormal];
button.backgroundColor = [UIColor redColor];
[self.view addSubview:button];
@weakify(self);
[button mas_makeConstraints:^(MASConstraintMaker *make) {
@strongify(self);
make.size.mas_equalTo(CGSizeMake(self.view.bounds.size.width, 30));
make.bottom.equalTo(self.view).offset(0);
}];
// 監(jiān)聽事件
// 把按鈕點(diǎn)擊事件轉(zhuǎn)換為信號,點(diǎn)擊按鈕,就會發(fā)送信號
[[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
LxPrintf(@"進(jìn)來了");
RACViewController *VC = [[RACViewController alloc] init];
@strongify(self);
[self presentViewController:VC animated:YES completion:^{
LxPrintf(@"%s",__func__);
}];
}];
}
#pragma mark --手勢
-(void)addTap
{
/**************手勢*****************/
self.view.userInteractionEnabled = YES ;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
[[tap rac_gestureSignal] subscribeNext:^(id x) {
LxDBAnyVar(x);
}];
[self.view addGestureRecognizer:tap];
}
#pragma mark --通知中心
-(void)notifiCenter
{
/**************通知*****************/
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIApplicationDidEnterBackgroundNotification object:nil] subscribeNext:^(id x) {
LxDBAnyVar(x);
}];
}
#pragma mark --代理
-(void)delegateDemo
{
UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"RAC" message:@"ReactiveCocoa" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ensure", nil nil];
//方法一:代理
// [[self rac_signalForSelector:@selector(alertView:clickedButtonAtIndex:) fromProtocol:@protocol(UIAlertViewDelegate)] subscribeNext:^(id x) {
//
// RACTuple *tuple = (RACTuple *)x ; //類似于swift的元組
//
// LxDBAnyVar(tuple);
//
// LxDBAnyVar(tuple.first);
// LxDBAnyVar(tuple.second);
// LxDBAnyVar(tuple.third);
//
// }];
[alterView show];
//方法二:內(nèi)部封裝的方法
//更簡單的方式
[[alterView rac_buttonClickedSignal] subscribeNext:^(id x) {
LxDBAnyVar(x);
}];
}
#pragma mark -- KVO
-(void)addKvo
{
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.delegate = (id<UIScrollViewDelegate>)self ;
[self.view addSubview:scrollView];
UIView *scrollViewContentView = [[UIView alloc] init];
scrollViewContentView.backgroundColor = [UIColor yellowColor];
[scrollView addSubview:scrollViewContentView];
@weakify(self);
[scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
@strongify(self);
make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(80, 80, 80, 80));
}];
[scrollViewContentView mas_makeConstraints:^(MASConstraintMaker *make) {
@strongify(self);
make.edges.equalTo(scrollView);
make.size.mas_equalTo(CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));
}];
[RACObserve(scrollView, contentOffset) subscribeNext:^(id x) {
LxDBAnyVar(x);
}];
}
//處理當(dāng)界面有多次請求時,需要都獲取到數(shù)據(jù)時,才能展示界面
-(void)moreRequest
{
/*
1.rac_liftSelector:withSignalsFromArray:Signals:當(dāng)傳入的Signals(信號數(shù)組),每一個signal都至少sendNext過一次,就會去觸發(fā)第一個selector參數(shù)的方法。
2.使用注意:幾個信號,參數(shù)一的方法就幾個參數(shù),每個參數(shù)對應(yīng)信號發(fā)出的數(shù)據(jù)。
*/
//處理多個請求,都返回結(jié)果的時候,統(tǒng)一做處理.
RACSignal *request1 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
// 發(fā)送請求1
[subscriber sendNext:@"發(fā)送請求1"];
return nil;
}];
RACSignal *request2 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
// 發(fā)送請求2
[subscriber sendNext:@"發(fā)送請求2"];
return nil;
}];
// 使用注意:幾個信號,參數(shù)一的方法就幾個參數(shù),每個參數(shù)對應(yīng)信號發(fā)出的數(shù)據(jù)。
[self rac_liftSelector:@selector(updateUIWithR1:r2:) withSignalsFromArray:@[request1,request2]];
}
// 更新UI
- (void)updateUIWithR1:(id)data r2:(id)data1
{
NSLog(@"更新UI%@ %@",data,data1);
}
#pragma mark --定時器
-(void)RACSchedulerAndMain
{
/**************定時器*****************/
//1.延遲某個時間再做某件事
// [[RACScheduler mainThreadScheduler] afterDelay:2 schedule:^{
//
// LxDBAnyVar(rac);
//
// }];
//2.每間隔多長時間做一件
[[RACSignal interval:1 onScheduler:[RACScheduler mainThreadScheduler]] subscribeNext:^(id x) {
LxDBAnyVar(x);
// LxPrintAnything(@"55555555");
}];
//這是定時器最常用的兩種方法,第一種方法,延遲時間去做某件事,更改afterDelay的屬性,第二種方法,每間隔躲藏時間去做一件事,更改interval的屬性
}
注:轉(zhuǎn)載自http://blog.csdn.net/y_csdnblog_xx/article/details/51480068