Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind two UIview fram/Position using Rxswift

I want to change view2 position automatically when view1 position will change and bind the both view position using Rxswift.

I try to observe view frame/position using this

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

it print frame on init time but when change view position using constraints

self.constraintHorizontalCenterView.constant = 1000

it print nothing means this code not observe view position...

Is there any way to observe continuously view position or bind view position?

like image 285
Chirag Desai Avatar asked May 17 '17 12:05

Chirag Desai


Video Answer


2 Answers

Whether the frame reflect the actual location or not, you could observe with frame like this.

This is old post, but I share my code. It works correctly.

   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)

In here, I tried to observe the mapView's frame with using Driver.

Hope this help to you.

like image 146
Hwangho Kim Avatar answered Oct 27 '22 08:10

Hwangho Kim


from apple '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.

You can observe bound or center to instead.

like image 1
Magic Avatar answered Oct 27 '22 07:10

Magic