6

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

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

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

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

self.constraintHorizontalCenterView.constant = 1000

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

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

4

3 回答 3

1

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

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

   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的框架。

希望这对您有所帮助。

于 2020-03-04T01:07:18.003 回答
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。

于 2017-05-23T08:39:03.777 回答
-1

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

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

于 2017-05-17T13:59:31.483 回答