有没有一种方法可以自动检测 UIView 何时改变它在屏幕上的表观比例,因为它的父级之一改变了比例?请注意,我不是在问如何检测 UIView 的自己的 比例属性是否发生变化,而是它的父级之一的变化?
最佳答案
是的。有几种方法可以做到这一点(甚至不用搜索,我就知道我们可以通过 KVO、RxSwift 或 ReactiveCocoa 来做到这一点。)。所以,是的,如果您不喜欢响应式编程,请使用 KVO。但我也在想你可以只使用你的父 View 类发送的 NotificationCenter。我忘记了我在想的另一种方式,我正要在这里打字,但它不见了,很难过。
无论如何,例如:
[self.view addObserver:self forKeyPath:@"frame" options:0 context:NULL];
[self.view.layer addObserver:self forKeyPath:@"bounds" options:0 context:NULL];
[self.view.layer addObserver:self forKeyPath:@"transform" options:0 context:NULL];
[self.view.layer addObserver:self forKeyPath:@"position" options:0 context:NULL];
[self.view.layer addObserver:self forKeyPath:@"zPosition" options:0 context:NULL];
[self.view.layer addObserver:self forKeyPath:@"anchorPoint" options:0 context:NULL];
[self.view.layer addObserver:self forKeyPath:@"anchorPointZ" options:0 context:NULL];
[self.view.layer addObserver:self forKeyPath:@"frame" options:0 context:NULL];
代码块来自这个答案:https://stackoverflow.com/a/19687115/3231194
希望对您有所帮助!
关于ios - 如何让 UIView 检测到父尺度的变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52896617/