rxswift

作者: 老猫_2017 | 来源:发表于2020-04-27 10:14 被阅读0次
    1. vm data -> UI
    
    viewModel.signingIn
      .bind(to: signingUpOulet.rx.isAnimating)
      .disposed(by: disposeBag)
    
    
    viewModel.signupEnabled
                .subscribe(onNext: { [weak self] valid  in
                    self?.signupOutlet.isEnabled = valid
                    self?.signupOutlet.alpha = valid ? 1.0 : 0.5
                })
                .disposed(by: disposeBag)
    
    // 自定义rx
    viewModel.validatedUsername
                .bind(to: usernameValidationOutlet.rx.validationResult)
                .disposed(by: disposeBag)
    
    extension Reactive where Base: UILabel {
        var validationResult: Binder<ValidationResult> {
            return Binder(base) { label, result in
                label.textColor = result.textColor
                label.text = result.description
            }
        }
    }
    
    // 手势
    let tapBackground = UITapGestureRecognizer()
            tapBackground.rx.event
                .subscribe(onNext: { [weak self] _ in
                    self?.view.endEditing(true)
                })
                .disposed(by: disposeBag)
            view.addGestureRecognizer(tapBackground)
    
    // ui -》 vm 操作处理
    usernameOutlet.rx.text.orEmpty.asObservable()
                .flatMapLatest { username in
                    return validationService.validateUsername(username)
                        .observeOn(MainScheduler.instance)
                        .catchErrorJustReturn(.failed(message: "Error contacting server"))
                }
                .share(replay: 1)
    
    
    1. observable 类型,可以被观察

    相关文章

      网友评论

          本文标题:rxswift

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