美文网首页
RAC实战4---基本元素的使用

RAC实战4---基本元素的使用

作者: 小怪兽鱼小宝 | 来源:发表于2019-04-29 18:15 被阅读0次

    我们这节从基本的一些元素或者控件的使用方式说起。

    1.按钮。

    在storyboard中新建一个按钮,对应到viewcontroller中的btn1属性,直接代码创建也是一样。在viewdidload中或者放到一个单独的方法来。

        [[self.btn1 rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            NSLog(@"%@",x);
            NSLog(@"按钮1被按下");
        }];
    

    下面看看源码(UIControl+RACSignalSupport.m,这个是UIButton基类的分类)是怎么实现这个功能的:

    - (RACSignal *)rac_signalForControlEvents:(UIControlEvents)controlEvents {
        @weakify(self);
    
        return [[RACSignal
            createSignal:^(id<RACSubscriber> subscriber) {
                @strongify(self);
    
                [self addTarget:subscriber action:@selector(sendNext:) forControlEvents:controlEvents];
    
                RACDisposable *disposable = [RACDisposable disposableWithBlock:^{
                    [subscriber sendCompleted];
                }];
                [self.rac_deallocDisposable addDisposable:disposable];
    
                return [RACDisposable disposableWithBlock:^{
                    @strongify(self);
                    [self.rac_deallocDisposable removeDisposable:disposable];
                    [self removeTarget:subscriber action:@selector(sendNext:) forControlEvents:controlEvents];
                }];
            }]
            setNameWithFormat:@"%@ -rac_signalForControlEvents: %lx", RACDescription(self), (unsigned long)controlEvents];
    }
    

    首先这个方法用createSignal创建了一个信号,参数是一个block,这些之前的文章中有详细介绍。最后方法返回的也是个信号。所以可以订阅这个信号。接下来 [self addTarget:subscriber action:@selector(sendNext:) forControlEvents:controlEvents];这个self也就是btn1,其实就是给btn1添加事件处理方法。controlEvents就是UIControlEventTouchUpInside,也就是点击。事件的处理方法是subscriber的sendNext:方法。也就是当按钮点击的时候会触发subscriber的sendNext:方法,也就是发送信号。于是subscribeNext:后面的block会执行。

    2.通知

    如果接收一个通知可以使用下面的方法:

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(respondsToNotification:) name:@"noti" object:nil];
    

    另外还需要单独写一个处理方法

    - (void)respondsToNotification:(NSNotification *)noti {
        id obj = noti.object;
        NSDictionary *dic = noti.userInfo;
        NSLog(@"\n- self:%@ \n- obj:%@ \n- notificationInfo:%@", self, obj, dic);
    }
    

    那我们看看用ARC怎么实现呢

       [[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"noti" object:nil] subscribeNext:^(NSNotification * _Nullable x) {
            NSLog(@"%@",x);
        }];
    

    一处代码就搞定。代码简洁了不少。下面看看是怎么实现这个功能的。

    - (RACSignal *)rac_addObserverForName:(NSString *)notificationName object:(id)object {
        @unsafeify(object);
        return [[RACSignal createSignal:^(id<RACSubscriber> subscriber) {
            @strongify(object);
            id observer = [self addObserverForName:notificationName object:object queue:nil usingBlock:^(NSNotification *note) {
                [subscriber sendNext:note];
            }];
    
            return [RACDisposable disposableWithBlock:^{
                [self removeObserver:observer];
            }];
        }] setNameWithFormat:@"-rac_addObserverForName: %@ object: <%@: %p>", notificationName, [object class], object];
    }
    

    同样是用createSignal创建了信号,传递了block参数。block中调用[NSNotificationCenter defaultCenter] 的addObserverForName: object: queue: usingBlock:方法添加了观察者。订阅信号的时候,这个block会执行。等有消息的时候,subscribeNext后面的block会执行,打印消息的相关内容。

    3.监听textfield输入内容

    textfield的内容一有变化,就会执行后面的block,打印出textfield里的内容。

        [[self.textField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
            NSLog(@"%@",x);
        }];
    

    但是,还有另外一个更简洁的写法

    RAC(_label,text) = _textField.rac_textSignal;
    

    其中RAC是一个宏,宏的用法:
    RAC(对象,对象的属性) = (一个信号);
    比如:RAC(btn,enable) = (RACSignal) 按钮的enable等于一个信号。

    4.代替代理

    代理我们在开发的时候用的是比较多的.我们定义一个新的控件MyView,上面有个button,当点击button的时候,会向使用这个控件的窗口发送一个消息。知道这个控件上的按钮被点击了。

    MyView.h
    @class MyView;
    
    @protocol MyViewDelegate <NSObject>
    -(void)myViewClick:(MyView*)myView;
    @end
    
    @interface MyView : UIView
    @property(nonatomic,weak) id<MyViewDelegate>  delegate;
    @end
    
    MyViwe.m
    #import "MyView.h"
    
    @implementation MyView
    
    -(instancetype)initWithFrame:(CGRect)frame{
        if (self = [super initWithFrame:frame]) {
            UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame =  CGRectMake(10, 10, 180, 20);
            [button setTitle:@"代替delegate" forState:UIControlStateNormal];
            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            self.backgroundColor = [UIColor greenColor];
            [self addSubview:button];
        }
        return self;
    }
    
    -(IBAction)buttonClick:(id)sender{
        if ([self.delegate respondsToSelector:@selector(myViewClick:)]) {
            [self.delegate myViewClick:self];
        }
    }
    
    使用的地方,遵守MyViewDelegate协议,实现下面的方法。
    -(void)myViewClick:(MyView*)myView{
        NSLog(@"button clicked!");
    }
    
    

    下面通过例子看看怎么用RAC实现代理功能。

    MyView.h
    #import <ReactiveObjC/ReactiveObjC.h>    //添加头文件
    
    @property (nonatomic,strong) RACSubject *btnClickSignal;        //添加一个属性
    
    MyView.m
    //定义属性懒加载方法
    - (RACSubject *)btnClickSignal{             //RAC
        if (!_btnClickSignal) {
            _btnClickSignal = [RACSubject subject];
        }
        return _btnClickSignal;
    }
    
    //在button的点击处理方法里添加
    [_btnClickSignal sendNext:@"我可以代替代理哦"];     //RAC
    
    在使用MyView的ViewController里。_myView是MyView类型的对象。下面代码可以放到viewDidLoad里。
        [_myView.btnClickSignal subscribeNext:^(id  _Nullable x) {
            NSLog(@"%@",x);
        }];
    

    整个过程就是:实例化了一个RACSubject,其实继承自RACSignal的,但是可以发送信号sendNext。然后在viewController中订阅这个RACSubject类型对象的信号,在按钮点击的时候,会发送信号。于是subscribeNext后面的block会得到执行。

    5.KVO

    在storyboard中,创建一个UIView,链接到bgView属性。
    先看看我们不使用RAC的时候是如何实现的

    //让self观察_bgView的属性frame的变化。可以放到viewDidLoad中。
        [_bgView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
    
    //值变化时,下面的方法被执行
    -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
        NSLog(@"%@",keyPath);
        NSLog(@"%@",object);
        NSLog(@"%@",change);
        NSLog(@"%@",context);
        }
    

    用RAC实现KOV的代码如下:

    - (void)repleacKVO{
        [self.bgView rac_observeKeyPath:@"frame" options:NSKeyValueObservingOptionNew observer:nil block:^(id value, NSDictionary *change, BOOL causedByDealloc, BOOL affectedOnlyLastComponent) {
            NSLog(@"1 - %@",value);
        }];
    
    }
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        _bgView.frame = CGRectMake(50, 60, 200, 200);
    }
    

    在ViewController中监听了bgView的frame属性。当属性变化时,block被执行。
    上面的写法还是有些繁琐,其实还有更简单的写法

    //方法2
        [[self.bgView rac_valuesForKeyPath:@"frame" observer:nil] subscribeNext:^(id  _Nullable x) {
            NSLog(@"2 - %@",x);
        }];
    

    也可以这么写:

    //方法3
        [RACObserve(self.bgView, frame) subscribeNext:^(id  _Nullable x) {
            NSLog(@"3 - %@",x);
        }];
    

    通过打印的log可以看到,我运行程序写法二、三就打印了数据,但是写法一是等到值改变了再打印的数据。

    6.监听方法

    在上面的bgview上添加一个button。添加一个事件处理方法,并关联:

    -(IBAction)btnClick:(id)sender{
        NSLog(@"点击了按钮!");
    }
    
    //可以把下面的方法放到viewDidLoad中
        [[self rac_signalForSelector:@selector(btnClick:)] subscribeNext:^(RACTuple * _Nullable x) {
            NSLog(@"你竟然响应我了 厉害了");
            NSLog(@"%@",x);
        }];
    
    

    rac_signalForSelector:这个方法的作用就是将一个对象的方法触发事件转换成信号,通过对该信号的订阅获取该方法执行的时机。其他例子

    //
        [[self rac_signalForSelector:@selector(touchesBegan:withEvent:)] subscribeNext:^(RACTuple * _Nullable x) {
            NSLog(@"View 被touch了!");
        }];
      //  
        [[self rac_signalForSelector:@selector(repleacKVO)] subscribeNext:^(RACTuple * _Nullable x) {
            NSLog(@"repleacKVO 被调用!");
        }];
    

    都是把方法转换成了信号,然后订阅这个信号,以后方法调用的时候,block就会执行。

    参考
    https://www.jianshu.com/p/cd4031fbf8ff

    相关文章

      网友评论

          本文标题:RAC实战4---基本元素的使用

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