I have multiple UIView
s that are dependant on each other, a UIView
can change its frame.
Other UIView
s respond accordingly to the currently changing UIView
.
however, a UIView
changes its frame with animations, I tried to observe a UIView
's frame
property but it gives me the last result after it starts animating.
Here's my current setup:
override func viewDidLoad() {
super.viewDidLoad()
view.addObserver(self, forKeyPath: "frame", options: [NSKeyValueObservingOptions.old, .new, .prior, .initial], context: nil)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if let obj = object as? UIView {
print(obj.layer.frame.minY)
}
}
Example of the result when I observed the obj.layer.frame.minY
:
622.0
252.0
My problem is:
I want to observe the current obj.layer.frame.minY
while animating.
Update:
I tried to use the presentation() method, but I still get the same result. Here's how I do it:
override func viewDidLoad() {
super.viewDidLoad()
view.addObserver(self, forKeyPath: "frame", options: NSKeyValueObservingOptions.new, context: nil)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
// print("value of \(keyPath) changed: \(change![NSKeyValueChangeKey.newKey])")
if let obj = object as? UIView {
guard let presentation = obj.layer.presentation() else { return }
let a = presentation
print(a)
}
}