ios - 如何让 UIView 检测到父尺度的变化?

标签 ios cocoa-touch uikit core-animation

有没有一种方法可以自动检测 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/

相关文章:

iphone - 从窗口中移除时 UIScrollview contentOffset 发生变化

ios - 使用常用方法创建类

iphone - iOS : Call a method just one time

objective-c - Cocoa 线程安全可变集合访问

ios - 更改 UIAlertview 和 UIActionsheet 按钮的色调

ios - 如何在 iphone 上制作闪烁(或闪烁)的光标?

ios - 自定义快速相机圈触摸响应

ios - 使用iOS MapKit显示路况和路况时间

ios - 如何在其蒙版周围绘制 CALayer 边框?

ios - fatal error : unexpectedly found nil while unwrapping an Optional value (lldb) on uitextfield