1.给按钮绑定方法
[[self.button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
NSLog(@"%@",x);
}];
2.添加代理方法
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"RAC" message:@"RAC TEST" delegate:self cancelButtonTitle:@"CANCEL" otherButtonTitles:@"other", @"1111",@"2222",nil];
[[self rac_signalForSelector:@selector(alertView:clickedButtonAtIndex:)fromProtocol:@protocol(UIAlertViewDelegate)]subscribeNext:^(RACTuple *tuple) {
NSLog(@"tuple%@",tuple);
}];
[[alertView rac_buttonClickedSignal]subscribeNext:^(id x) {
NSLog(@"%@",x);
}];
3.接收通知
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"postData" object:nil]subscribeNext:^(NSNotification * notification) {
NSLog(@"%@",notification.name);
NSLog(@"%@",notification.object);
}];
4.KVO
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 300, 400, 300)];
scrollView.backgroundColor = [UIColor redColor];
scrollView.contentSize = CGSizeMake(0, 1000);
[self.view addSubview:scrollView];
[RACObserve(scrollView, contentOffset)subscribeNext:^(id x) {
NSLog(@"%@",x);
}];
MAP
映射的意思,就是创建一个订阅者的映射并且返回数据
我理解为拦截到信号内容,返回想要的内容
[[[self.button rac_signalForControlEvents:UIControlEventTouchUpInside]map:^id(id value) {
NSLog(@"value: %@",value);
return @"aaaa";
}]subscribeNext:^(id x) {
NSLog(@"x:%@",x);
}];
输出
2017-03-08 09:52:59.566726 RACDemo[6241:2176773] value: >
2017-03-08 09:52:59.567147 RACDemo[6241:2176773] x:aaaa
filter 过滤
在 filter 中,如果返回 yes 则发送信号,no 则取消发送,达到过滤的效果
[[self.textField.rac_textSignal filter:^BOOL(id value) {
return [value length] > 3;
}]subscribeNext:^(id x) {
NSLog(@"x:%@",x);
}];
输出
2017-03-08 10:06:27.552855 RACDemo[6257:2180433] x:1111
2017-03-08 10:06:28.166590 RACDemo[6257:2180433] x:11111
2017-03-08 10:06:28.397429 RACDemo[6257:2180433] x:111111
take
选取前几个信号进行发送
take 2 选取前两个信号发送
[[self.button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
[self.textField endEditing:YES];
// [alertView show];
//创建信号
RACSignal *signal = [[RACSignal createSignal:^RACDisposable *(id subscriber) {
[subscriber sendNext:@"1"];
[subscriber sendNext:@"2"];
[subscriber sendNext:@"3"];
[subscriber sendNext:@"4"];
[subscriber sendCompleted];
return nil;
}] take:2];
//发送信号
[signal subscribeNext:^(id x) {
NSLog(@"x : %@",x);
} completed:^{
NSLog(@"completed");
}];
}];
输出
2017-03-08 10:15:11.815895 RACDemo[6274:2184570] x : 1
2017-03-08 10:15:11.816197 RACDemo[6274:2184570] x : 2
2017-03-08 10:15:11.816323 RACDemo[6274:2184570] completed
skip
跳过信号
把上文中 take
替换成 skip
输出:
2017-03-08 10:18:16.362060 RACDemo[6281:2185871] x : 3
2017-03-08 10:18:16.362506 RACDemo[6281:2185871] x : 4
2017-03-08 10:18:16.362658 RACDemo[6281:2185871] completed
repeat
把 take 2
替换成 repeat
信号会无限制的重复发送
相似的还有takeLast
takeUntil
takeWhileBlock
skipWhileBlock
skipUntilBlock
repeatWhileBlock
都可以根据字面意思来理解。
delay
延迟发送信号
//创建信号
RACSignal *signal = [[[RACSignal createSignal:^RACDisposable *(id subscriber) {
[subscriber sendNext:@"1"];
[subscriber sendNext:@"2"];
[subscriber sendNext:@"3"];
[subscriber sendNext:@"4"];
[subscriber sendCompleted];
return nil;
}] delay:3]take:3];
//发送信号
[signal subscribeNext:^(id x) {
NSLog(@"x : %@",x);
} completed:^{
NSLog(@"completed");
}];
输出
2017-03-08 10:24:25.470258 RACDemo[6294:2188587] x : 1
2017-03-08 10:24:25.470626 RACDemo[6294:2188587] x : 2
2017-03-08 10:24:25.470870 RACDemo[6294:2188587] x : 3
2017-03-08 10:24:25.471233 RACDemo[6294:2188587] completed
throttle
加了节流管道,后面跟上了类型为NSTimeInterval的参数后,只有0.5S内信号不产生变化才会发送请求,这样快速的输入也不会造成多次输出。
[[[self.textFild rac_textSignal] throttle:0.5] subscribeNext:^(id x) {
NSLog(@"%@", x);
}];
distinctUntilChanged
网络请求中为了减轻服务器压力,无用的请求我们应该尽可能不发送。distinctUntilChanged的作用是使RAC不会连续发送两次相同的信号,这样就解决了这个问题。
[[[self.textFild rac_textSignal] distinctUntilChanged] subscribeNext:^(id x) {
NSLog(@"%@", x);
}];
timeout
超时信号,当超出限定时间后会给订阅者发送error信号
//创建信号 延迟3秒发送,但是信号时限为2秒
RACSignal *signal = [[[[RACSignal createSignal:^RACDisposable *(id subscriber) {
[subscriber sendNext:@"1"];
[subscriber sendNext:@"2"];
[subscriber sendNext:@"3"];
[subscriber sendNext:@"4"];
[subscriber sendCompleted];
return nil;
}] delay:3]take:3]timeout:2 onScheduler:[RACScheduler mainThreadScheduler]];
//发送信号
[signal subscribeNext:^(id x) {
NSLog(@"x: %@",x);
} error:^(NSError *error) {
NSLog(@"error : %@",error);
}];
输出
2017-03-08 10:32:32.149072 RACDemo[6302:2191123] error : Error Domain=RACSignalErrorDomain Code=1 "(null)"
ignore
忽略信号,指定一个任意类型的量(可以是字符串,数组等),当需要发送信号时讲进行判断,若相同则该信号会被忽略发送。
//创建信号 take 3 发送三个信号,但是忽略内容为@"1"的信号
RACSignal *signal = [[[RACSignal createSignal:^RACDisposable *(id subscriber) {
[subscriber sendNext:@"1"];
[subscriber sendNext:@"2"];
[subscriber sendNext:@"3"];
[subscriber sendNext:@"4"];
[subscriber sendCompleted];
return nil;
}]take:3]ignore:@"1"];
//发送信号
[signal subscribeNext:^(id x) {
NSLog(@"x: %@",x);
} completed:^{
NSLog(@"completed");
}];
输出
2017-03-08 10:38:36.399691 RACDemo[6313:2193199] x: 2
2017-03-08 10:38:36.400032 RACDemo[6313:2193199] x: 3
2017-03-08 10:38:36.400198 RACDemo[6313:2193199] completed
ignore 可以连续使用
//创建信号
RACSignal *signal = [[[[RACSignal createSignal:^RACDisposable *(id subscriber) {
[subscriber sendNext:@"1"];
[subscriber sendNext:@"2"];
[subscriber sendNext:@"3"];
[subscriber sendNext:@"4"];
[subscriber sendCompleted];
return nil;
}]take:3]ignore:@"1"]ignore:@"2"];
//发送信号
[signal subscribeNext:^(id x) {
NSLog(@"x: %@",x);
} completed:^{
NSLog(@"completed");
}];
输出
2017-03-08 10:39:07.841313 RACDemo[6316:2193759] x: 3
2017-03-08 10:39:07.841681 RACDemo[6316:2193759] completed
参拷贝:
http://www.jianshu.com/p/ff79a5ae0353
http://www.jianshu.com/p/aa155560bfed