0%

ReactiveCocoa的初步用法

ReactiveCocoa的初步用法

1.创建UI便于测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//创建一个UITextField
UITextField *textFild = [[UITextField alloc]init];
textFild.backgroundColor = [UIColor orangeColor];
[self addSubview:textFild];
[textFild mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(10);
make.top.mas_equalTo(64 + 5);
make.width.mas_equalTo(100);
make.height.mas_equalTo(50);
}];
//创建一个UIButton
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor redColor];
[self addSubview:button];
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(textFild.mas_bottom).offset(5);
make.left.equalTo(textFild);
make.width.height.mas_equalTo(50);
}];
//创建一个UIView
UIView *view = [[UIView alloc]init];
view.backgroundColor = [UIColor brownColor];
[self addSubview:view];
[view mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(button.mas_bottom).offset(5);
make.left.equalTo(button);
make.width.height.mas_equalTo(50);
}];
//创建一个UIScrollView
UIScrollView *sc = [[UIScrollView alloc]init];
[self addSubview:sc];
[sc mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(textFild.mas_top);
make.width.equalTo(@50);
make.height.equalTo(@200);
make.right.equalTo(@(-50));
}];
sc.contentSize = CGSizeMake(50, 800);
sc.backgroundColor = [UIColor redColor];
sc.delegate = self;

2.监听textField的触发事件

1
2
3
4
5
6
7
8
[[textFild rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
NSLog(@"----");
}];

[textFild.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
// NSLog(@"=======");
NSLog(@"%@",textFild.text);
}];

3.监听Button的点击事件

1
2
3
4
5
6
[[button rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
NSLog(@"clicking");
if ([_delegate respondsToSelector:@selector(getClickButtonPostTextFieldText:)]) {
[_delegate getClickButtonPostTextFieldText:textFild.text];
}
}];

4.动态修改UIButton的背景颜色

1
2
3
4
5
[[textFild.rac_textSignal map:^id _Nullable(NSString * _Nullable value) {
return (value.length >= 3) ? [UIColor redColor] : [UIColor brownColor] ;
}]subscribeNext:^(id _Nullable x) {
button.backgroundColor = x;
}];

5.监听手势点击

1
2
3
4
5
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
[view addGestureRecognizer:tap];
[[tap rac_gestureSignal]subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
NSLog(@"Tap Click");
}];

6.监听代理

1
2
3
4
5
6
7
8
[[self rac_signalForSelector:@selector(scrollViewWillBeginDragging:) fromProtocol:@protocol(UIScrollViewDelegate)] subscribeNext:^(id  _Nullable x) {//这里的x是表示 当页面有多个UIScrollView时,可以做判断是哪一个
// NSLog(@"点击了");
// NSLog(@"--- %@ ---",[x first]);
}];

[[self rac_signalForSelector:@selector(scrollViewDidEndDecelerating:) fromProtocol:@protocol(UIScrollViewDelegate)] subscribeNext:^(id _Nullable x) {

}];

1.监听自定义的代理

1
2
3
[[self rac_signalForSelector:@selector(getClickButtonPostTextFieldText:) fromProtocol:@protocol(TestClickDelegate)] subscribeNext:^(id  _Nullable x) {
NSLog(@"******* %@ *****",[x first]);
}];

7.监听通知 -> 键盘弹出和隐藏的通知

1
2
3
4
5
6
7
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardWillShowNotification object:nil] subscribeNext:^(NSNotification * _Nullable x) {
NSLog(@"键盘弹出");
[[NSNotificationCenter defaultCenter] postNotificationName:@"SHOW_LEYBOARD" object:nil];
}];
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardWillHideNotification object:nil] subscribeNext:^(NSNotification * _Nullable x) {
NSLog(@"键盘隐藏");
}];

8.KVO –> 监听UIScrollView的滚动

1
2
3
[RACObserve(sc, contentOffset) subscribeNext:^(id  _Nullable x) {
NSLog(@"contentSize -- %@",x);
}];

9.timers

1.延时执行

1
2
3
[[RACScheduler mainThreadScheduler] afterDelay:5 schedule:^{
NSLog(@"这是延时5秒后执行的");
}];

2.定时执行

1
2
3
4
[[RACSignal interval:2 onScheduler:[RACScheduler mainThreadScheduler]]subscribeNext:^(NSDate * _Nullable x) {
NSLog(@"每隔2秒执行一次");
}];