【问题标题】:Bind two UIview fram/Position using Rxswift使用 Rxswift 绑定两个 UIview 框架/位置
【发布时间】:2017-10-16 21:50:58
【问题描述】:

我想在 view1 位置发生变化时自动更改 view2 位置,并使用 Rxswift 绑定两个视图位置。

我尝试使用这个来观察视图框架/位置

view.rx.observe(CGRect.self,"frame")
        .subscribe(onNext: {
            print($0 ?? (0,0))
        })

在初始化时打印帧但是当使用约束改变视图位置时

self.constraintHorizontalCenterView.constant = 1000

什么都不打印意味着这段代码没有观察视图位置...

有没有办法连续观察视图位置或绑定视图位置?

【问题讨论】:

    标签: ios swift uiview rx-swift rx-cocoa


    【解决方案1】:

    来自苹果'框架' // animatable. do not use frame if view is transformed since it will not correctly reflect the actual location of the view. use bounds + center instead.

    您可以改为观察 bound 或 center to。

    【讨论】:

      【解决方案2】:

      框架是否反映实际位置,您可以像这样用框架观察。

      这是旧帖子,但我分享我的代码。它工作正常。

         self.mapView
                  .rx
                  .observe(CGRect.self, #keyPath(UIView.frame))
                  .asDriver(onErrorJustReturn: CGRect.zero)
                  .drive(onNext: { (rect) in
                      log.debug("Frame changed to \(String(describing: rect))")
                  }).disposed(by: self.disposeBag)
      

      在这里,我尝试使用 Driver 观察 mapView 的框架。

      希望对您有所帮助。

      【讨论】:

        【解决方案3】:

        UIView.frame 不符合 KVO,因此您无法直接观察它。无论如何,这样做是不恰当的恕我直言。

        某些事件导致您的视图改变大小。请注意这一点。

        【讨论】:

        • 其实frame是KVO。
        • "UIKit 中的任何内容都不能保证与 KVO 兼容。如果您碰巧发现 KVO-ing 属性有效,请不胜感激,这是无意的。另外:要小心。它很可能会闯入未来。” ——戴夫·德隆。这句话来自他在苹果公司工作的时候。他从 2010 年到 2017 年在苹果公司工作。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-05
        • 1970-01-01
        • 1970-01-01
        • 2016-09-26
        • 1970-01-01
        • 2010-12-16
        • 2017-12-27
        相关资源
        最近更新 更多