使用 Rxswift 绑定两个 UIview fram/Position



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

我尝试使用它观察视图框架/位置

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

它在初始化时打印帧,但当使用约束更改视图位置时

self.constraintHorizontalCenterView.constant = 1000

它打印什么都不意味着这个代码不观察视图位置...

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

无论帧是否反映实际位置,您都可以像这样用框架进行观察。

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

   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)

在这里,我尝试使用驱动程序观察地图视图的框架。

希望这对您有所帮助。

来自苹果'frame' // 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.

您可以改为观察绑定或居中。

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

某些事件导致视图更改大小。相反,请观察这一点。

最新更新