美文网首页
Reactivecocoa 6.0

Reactivecocoa 6.0

作者: 文子飞_ | 来源:发表于2021-03-26 22:39 被阅读0次

    一.UITextField输入文本监听

    1.假如你的界面有一个UITextField文本框,你需要对其输入内容进行监听。

    accountTextField.reactive.continuousTextValues.observeValues { (text) in
    
                        print(text ?? "")
    
                    }
    
    

    2.假如你不想知道UITextField的文本内容,而是想知道文本的长度,那么使用map函数进行信号内容的修改,然后再对map后的信号进行观察.map函数可以对信号的内容进行转换,他的返回值可以是任何你想要的类型.

     accountTextField.reactive.continuousTextValues.map { (text) -> Int in
    
                return text!.characters.count
    
            }.observeValues { (count) in
    
                print(count)
    
            }
    
    

    3.也许你只是在某个条件下,才想监听UITextfield文本的内容。比如:当文本的内容长度大于3的时候,你才想监听文本的内容。这时可以使用filter函数进行过滤操作。filter函数只能返回Bool类型。只有当filter函数返回true的时候,信号继续传递,我们才能监听到文本的内容;当filter函数返回False的时候,信号会被拦截。

    accountTextField.reactive.continuousTextValues.filter { (text) -> Bool in
    
                        return text!.characters.count > 3
    
                    }.observeValues { (text) in
    
                        print(text ?? "")
    
                    }
    
    

    二.UIButton点击事件监听.

     loginBtn.reactive.controlEvents(.touchUpInside).observeValues { (btn) in
    
                print("btn click")
    
            }
    
    

    三.Combine Siganl (组合信号).

    最经典的案例:登录界面的实现 一个账号文本框 一个密码文本框 当两个文本框内容的长度都大于3的时候 ,login按钮才可点击。

    //首先创建账号文本框的signal ,然后使用map函数对信号内容进行转换。结果转换为Bool类型
    let accountSignal = accountTextField.reactive.continuousTextValues.map { (text) -> Bool in
    
                return text!.characters.count > 3
            }
    //密码文本框同理
    let pwdSignal = pwdTextField.reactive.continuousTextValues.map { (text) -> Bool in
    
                return text!.characters.count > 3
    
            }
    //使用combineLatest函数将两个信号组合为一个信号,然后利用map函数对信号进行转换。使用<~将信号的结果绑定到loginBtn.
    loginBtn.reactive.isEnabled <~ accountSignal.combineLatest(with: pwdSignal).map({ $0 && $1
            })
    
    

    如果你对$不熟悉,可以这样写

     let accountSignal = accountTextField.reactive.continuousTextValues.map { (text) -> Bool in
    
                return text!.characters.count > 3
            }
    
     let pwdSignal = pwdTextField.reactive.continuousTextValues.map { (text) -> Bool in
    
                return text!.characters.count > 3
    
            }
    
     loginBtn.reactive.isEnabled <~ accountSignal.combineLatest(with: pwdSignal).map({ (accountValid, pwdValid) -> Bool in
                return accountValid && pwdValid
            })
    
    

    四.KVO VS MutableProperty

    如果你想对某个属性的value进行observe.那么你可以使用KVO ,当然Reactivecocoa里的MutableProperty也可以满足你的需求,而且它比KVO用起来更加方便。

    //<>里面可以是任意类型,它代表属性的类型。
    let racValue = MutableProperty<Int>(1)
    
    racValue.producer.startWithValues { (make) in
               print(make)
       }
    
     racValue.value = 10
    
    
    image

    五.方法调用拦截

    当你想获取到某个方法被调用的事件,比如UIViewController的ViewwillAppear事件。

     self.reactive.trigger(for: #selector(UIViewController.viewWillAppear(_:))).observeValues { () in
    
                print("viewWillAppear被调用了")
    
            }
    
    
    image

    六.监听对象的生命周期

    比如你想在某个对象被销毁以后,做一些事情。那么你可以对这个对象的生命周期进行监听,也就是当对象销毁的时候,你获得对象销毁的信号,然后观察这个信号。当然你也可以重写deinit函数,当对象被销毁的时候,会调用deinit函数。也就是Objective-C中的dealloc方法。

    //按钮点击 push到secondVC 
     loginBtn.reactive.controlEvents(.touchUpInside).observeValues { (btn) in
    
                let secondVC = SecondViewController()
                //当在secondVC pop的时候,secondVC会被销毁
                secondVC.reactive.lifetime.ended.observeCompleted {
    
                    print("secondVC 被销毁")
    
                }
    
                self.navigationController?.pushViewController(secondVC, animated: true)
    
            }
    
    

    再看下面的例子:

    image

    开始的时候给person属性赋值一个内存地址为0x60800005cfe0对象,
    然后对该对象的生命周期进行监听。然后给person属性重新赋值一个内存地址为0x6000002400c0对象。因为person指针指向了内存为0x6000002400c0的对象,所以这个时候内存为0x60800005cfe0的引用计数为0,所以0x60800005cfe0的对象会被销毁,而我们正好对内存为0x60800005cfe0对象的生命周期进行了监听,所以会获取“person对象被销毁了”事件。

    作者:嗨呀好开心
    链接:https://www.jianshu.com/p/c35ef5a4ab63
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

    相关文章

      网友评论

          本文标题:Reactivecocoa 6.0

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