导入篇
很多新手刚开始使用CocoaPods导入RAC的时候不声明版本
platform :ios, ‘8.0’
target ‘Test’ do
use_frameworks!
pod 'ReactiveCocoa'
end
其实上面那种非常是非常错的,除非你是用Swift来开发,如果你仅仅是只想要用OC来开发的话
platform :ios, ‘8.0’
target ‘Test’ do
use_frameworks!
pod 'ReactiveCocoa', '~> 2.5'
end
最后建议在你的PCH文件中加入下面一段代码,这样省去了大量的导入操作了,毕竟经常能用得到
#import <ReactiveCocoa/ReactiveCocoa.h>
基础用法
啥也不多说,直接上代码
// x-> self.tf
[[self.tf rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(id x) {
NSLog(@"tf");
}];
// x-> self.tf.text
[[self.tf rac_textSignal] subscribeNext:^(id x) {
NSLog(@"%@",x);
}];
// 按钮点击监听
[[self.btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
NSLog(@"btnDidClick");
}];
// 手势监听
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
[[tap rac_gestureSignal] subscribeNext:^(id x) {
NSLog(@"tap");
}];
self.lb.userInteractionEnabled = YES;
[self.lb addGestureRecognizer:tap];
// 通知
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"123321" object:nil] subscribeNext:^(NSNotification *notify) {
NSLog(@"%@",notify.object);
}];
[[NSNotificationCenter defaultCenter] postNotificationName:@"123321" object:@[@"1",@"2"]];
// KVO
UIScrollView *scr = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 300, 414, 736)];
scr.contentSize = CGSizeMake(414, 1000);
[self.view addSubview:scr];
[RACObserve(scr, contentOffset) subscribeNext:^(id x) {
}];
RACSignal
RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@"asdasd"];
[subscriber sendCompleted];
return nil;
}];
[signal subscribeNext:^(id x) {
NSLog(@"%@",x);
} completed:^{
NSLog(@"completed");
}];
网友评论