1.ReactiveCocoa(简称RAC),是GitHub上开源的一个应用于iOS和OS X开发的一个新框架.RAC具有函数式编程和响应者编程的特性
RAC是一套super重量级函数响应编程开源框架,它可以帮助我们简单粗暴的处理事件!!!
2.在实际开发中的使用
2.1UIButton
[[_btn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
NSLog(@"%@",x);
//在这里执行_btn点击后的code
}];
2.2UITextField
[[_textField rac_textSignal]subscribeNext:^(NSString * _Nullable x) {
NSLog(@"%@",x);
//_textField.text内容改变后的block回调
}];
2.3通知
[[[NSNotificationCenter defaultCenter]rac_addObserverForName:UIKeyboardDidShowNotification object:nil]subscribeNext:^(NSNotification * _Nullable x) {
NSLog(@"%@",x.userInfo);
}];
2.4Timer定时器
使用RAC来实现定时器,能够完美解决循环引用,内存不释放的问题,
//定时器
self.timerDisposable = [[RACSignal interval:2.0 onScheduler:[RACScheduler mainThreadScheduler]]subscribeNext:^(NSDate * _Nullable x) {
NSLog(@"我是定时器");
}];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(50 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//在合适的位置调用,释放定时器
[self.timerDisposable dispose];
});
2.5KVO
2.5.1监听对象的成员变量
@interface Person:NSObject
@property(nonatomic,strong) NSString * name;
@end
@implementation Person
@end
@interface ViewController ()
@property(nonatomic,strong) Person * person;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_person = [[Person alloc]init];
_person.name = @"小王";
//无需进行移除监听通知的操作,返回的RACDisposable对象会帮我们做这些操作
[_person rac_observeKeyPath:@"name" options:NSKeyValueObservingOptionNew observer:nil block:^(id value, NSDictionary *change, BOOL causedByDealloc, BOOL affectedOnlyLastComponent) {
NSLog(@"name修改了");
}];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
_person.name = @"小李";
NSLog(@"%@",_person.name);
}
2.5.2监听self的成员变量
//监听self.changeStr值得变化
[RACObserve(self, changeStr)subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];
2.6 合并信号
[[RACSignal combineLatest:@[[_textField rac_textSignal],[_passwordTextField rac_textSignal]] reduce:^id _Nonnull(NSString *name,NSString*password){
return [NSString stringWithFormat:@"%@+%@",name,password];
}]subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x); // x = name+password
}];
2.7 过滤
- (void)viewDidLoad {
[super viewDidLoad];
self.username = @"liuguanhua";
//过滤
//-filter 只有当block返回YES时才会创建一个新的RACSignal发送一个新值
//只有当名字的开头为"liu"时才打印
[[RACObserve(self, username)filter:^BOOL(id _Nullable value) {
return [value hasPrefix:@"liu"];
}]subscribeNext:^(id _Nullable newName) {
NSLog(@"%@",newName);
}];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
self.username = @"liuxiaoyun";
}
2.8 信号聚合
self.btn的enabled属性可以由self.textField.text以及self.passwordTextField的内容长度决定。
当username和password的长度都>=6时,返回YES.
RAC(self.btn, enabled) = [RACSignal
combineLatest:@[self.textField.rac_textSignal,self.passwordTextField.rac_textSignal]
reduce:^(NSString *name, NSString *password) {
NSLog(@"%@,%@",name,password);
return @(name.length>=6 && password.length>=6); //需要返回对象
}];
2.9 序列
//序列
NSArray *array = @[@"a",@"b",@"ab",@"agh",@"gha",@"rta",@"sda",@"asa",@"vba",@"nma",@(14),@(YES)];
[array.rac_sequence.signal subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];
NSDictionary *dict = @{
@"name":@"lgh",
@"age":@(14)
};
[dict.rac_sequence.signal subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];
2.10协议
//协议
[[self rac_signalForSelector:@selector(textFieldDidEndEditing:) fromProtocol:@protocol(UITextFieldDelegate)]subscribeNext:^(RACTuple * _Nullable x) {
NSLog(@"protocol:%@",x);
}];
self.textField.delegate = self;
2.11手势
//手势
UITapGestureRecognizer *reg = [[UITapGestureRecognizer alloc]init];
self.label.userInteractionEnabled = YES;
[self.label addGestureRecognizer:reg];
[[reg rac_gestureSignal]subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
NSLog(@"UIGestureRecognizer: %@",x);
}];
网友评论