【发布时间】:2019-03-24 14:19:15
【问题描述】:
有没有一种方法可以自动检测 UIView 何时改变它在屏幕上的明显比例,因为它的父母之一改变了比例?请注意,我不是在问如何检测 UIView 的 自己的 缩放属性是否发生变化,而是其父级之一的变化?
【问题讨论】:
标签: ios cocoa-touch uikit core-animation
有没有一种方法可以自动检测 UIView 何时改变它在屏幕上的明显比例,因为它的父母之一改变了比例?请注意,我不是在问如何检测 UIView 的 自己的 缩放属性是否发生变化,而是其父级之一的变化?
【问题讨论】:
标签: ios cocoa-touch uikit core-animation
是的。有几种方法可以做到这一点(甚至不用搜索,我知道我们可以通过 KVO、RxSwift 或 ReactiveCocoa. 做到这一点)。所以,是的,如果您不喜欢响应式编程,请使用 KVO。但我也认为你可以只使用由你的父视图类发送的 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
我希望这会有所帮助!
【讨论】: