rac 事件概述及处理

概述:
想要知道rac就必须知道rac中的三个要点 创建信号、发送信号、订阅信号。

  • reactive事件

reactive事件就是将控件变信号可响应的, continuousTextValues: 是textField的text值的信号, 通过observeValues,我们可以观察到continuousTextValues这个信号传来的Value事件,每次在usernameTextField输入的时候,我们就会接收到text的值。

/** textField 输入事件 */
tf_userName.reactive.continuousTextValues.observeValues {
    text in

    print(text ?? "")
 }
  • map函数

注意到我们新添加的map函数,给map函数提供一个closure,它就能够转换事件的数据。
对于每一次map接收到的Value事件,它就会运行closure,以closure的返回值作为Value事件发送出去。
面的代码中,我们的text的值映射成text的字符数。

    fileprivate func set_map() {
        txtUser Name.reactive.continuousTextValues.map({ text in
            return text?.characters.count
        }).filter { (characterCount) -> Bool in
            return characterCount! > 3
            }.observeValues { (characterCount) in
                log.debug(characterCount ?? "")
        }
    }

  • observeValues 使用

关于Observer上面我已经提过了, 有些observer我没有使用到, 大家可以继续摸索

    fileprivate func set_observeValues() {
        txtUserName.reactive.continuousTextValues.observeValues { (text) in
            log.debug(text ?? "")
        }
        
        txtPassword.reactive.continuousTextValues.observeValues { (text) in
            log.debug(text ?? "")
        }
    }

  • combineLatest联合信号
  1. 场景: 在项目中, 我们好多地方都需要好多条件同时成立的时候才做某些事情,combineLatest 信号就是为了解决多条件成立才执行而产生的。
  2. 使用:
Signal.combineLatest(self.rac_mobileIsValid.signal, self.rac_codeIsValid.signal, self.rac_hasAgreed.signal).observeValues { [unowned self] (value) in
            let isValid = value.0 && value.1 && value.2
            self.btn_binding.backgroundColor = isValid ? projectTintColor : .lightGray
            self.btn_binding.isEnabled = isValid
        }
  • filter 使用
    fileprivate func set_filter() {
        /** filter 这个函数只有返回true的时候才会调用 可以根据返回值进行业务逻辑处理 */
        txtUserName.reactive.continuousTextValues.filter({
            text in
            
            return text!.characters.count > 3 // 如果字符大于3的时候才会返回true
            
        }).observeValues {  // 通过observeValues, 我们可以获取到continuousTextValues这个信号传来的value事件
            text in
            
            print(text ?? "")
        }
    }

  • <~ 符号

<~操作符的左边应为遵循了BindingTarget的协议的类型,而右边是信号(Signal)的类型。

第一种方式,针对按钮操作
btn_login.reactive.isEnabled <~ Signal.combineLatest(validUserNameSignal, validPassWordNameSignal).map{ $0 && $1 }

第二种方式,针对需要处理的业务逻辑操作
Signal.combineLatest(rac_operatorReqComplete.signal, rac_priceListReqComplete.signal)
            .observeValues { (tuple) in
            if tuple.0 == true && tuple.1 == true {
                print("")
            }
        }
  • signal(for: )函数 监听系统方法
  1. 场景: 我们有时候对 有些 自定义的view点击需要收回键盘,例如: UICollectionView 点击的时候, 没办法直接获取 UICollectionViewtouchesBegan 方法进行重写, 这种情况下我们就需要使用到signal对象对view的 touchesBegan 进行监听
  2. 使用:
v_dataBase.cvc_leader.reactive.signal(for: #selector(touchesBegan(_:with:))).observeValues { (_) in
            `self`.v_dataBase.v_search.tf_search.resignFirstResponder()
        }
  • signal(forKeyPath: )函数 监听系统方法
  1. 场景: 针对 webviewcanGoBack 属性进行监听, 使用此函数, 能够对所有的对象属性进行监听操作。所监听到的值 valueAny? 类型。
  2. 使用:
self.webView.reactive.signal(forKeyPath: "canGoBack")
            .observeValues { (value) in
                guard let canGoBack = value as? Bool else { return }
                guard let `self` = weakSelf else { return }
                if canGoBack {
                    print("")
                } else { 
                    print("")
                }
        }

注意: 使用RAC添加到项目中的话, 需要谨慎使用,以免出现释放不了而产生的泄露问题。 今天先出基础, 后期继续更新封装网络请求。

  • 本人小白一个, 有不合适之处, 还请留言指教,谢谢!

你可能感兴趣的:(rac 事件概述及处理)