美文网首页
(四)、iOS RAC - RACUsagVC

(四)、iOS RAC - RACUsagVC

作者: Buddha_like | 来源:发表于2019-03-25 17:32 被阅读0次

pragma mark: <View 上的按钮点击事件>

[[self.usageView rac_signalForSelector:@selector(tapBtnAction:)] subscribeNext:^(RACTuple * _Nullable x) {
    NSLog(@"RACUsagVC:%@",x);
    NSLog(@"RACUsagVC:%ld",x.count);
    NSLog(@"RACUsagVC:%@",x[0]);
    UIButton *sender = (UIButton *)x[0];
    NSLog(@"xxxxxTag:%ld",sender.tag);
}];

pragma mark: <KVO>

[RACObserve(self.usageView, frame) subscribeNext:^(id  _Nullable x) {
    NSLog(@"2 - %@",x);
}];

pragma mark: <按钮点击>

[self.view addSubview:self.racBtn];
[[self.racBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
    NSLog(@"%@",x);
    NSLog(@"%ld",x.tag);
}];

 /**
 最关键的代码就是[self addTarget:subscriber action:@selector(sendNext:) forControlEvents:controlEvents];
 @param:self 就是btn本身,因为是btn调用的方法
 @param:然后target是subscriber(订阅者)
 @param:方法是 :sendNext:
 事件是传入的事件
 return:所以现在按钮的点击方法会通过subscriber去调用sendNext方法,我们之前有提到过,RACSignal,所以这个时候我们订阅他就可以拿到sendNext的值。
 */
 
 - (UsageView *)usageView {
if (!_usageView) {
    _usageView = [[UsageView alloc] init];
    _usageView.frame = CGRectMake(100, 100, 200, 200);
}
return _usageView;
}

- (UIButton *)racBtn {
if (!_racBtn) {
    _racBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _racBtn.backgroundColor = [UIColor yellowColor];
    _racBtn.tag = 999;
    _racBtn.frame = CGRectMake(100, 459, 80, 80);
}
return _racBtn;
}

//UsageView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
    self.backgroundColor = [UIColor grayColor];
    [self addSubview:self.tapBtn];
}
return self;
}

- (void)tapBtnAction:(UIButton *)sender {
NSLog(@"按钮响应事件%@",sender);
}

- (UIButton *)tapBtn {

if (!_tapBtn) {
    _tapBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _tapBtn.frame = CGRectMake(40, 40, 60, 60);
    _tapBtn.backgroundColor = [UIColor blueColor];
    _tapBtn.tag = 1000;
    [_tapBtn addTarget:self action:@selector(tapBtnAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _tapBtn;
}

相关文章

网友评论

      本文标题:(四)、iOS RAC - RACUsagVC

      本文链接:https://www.haomeiwen.com/subject/ubpbvqtx.html