gpt4 book ai didi

ios - Xcode 如何检测一个 View 的属性改变了?

转载 作者:行者123 更新时间:2023-11-29 01:53:05 25 4
gpt4 key购买 nike

如何检测 View 的 x、y、宽度或高度已更改?

我想实现一旦一个 View 的属性发生变化,另一个 View 的位置就会重新组织。

例如,我有一个具有动态高度的 WebView 。每次 webViewDidFinishLoad 我都会改变 webView 的高度,然后另一个 View 的位置将根据 webview.frame 改变。

我一直在想我更改委托(delegate)函数 webViewDidFinishLoad 中所有 View 的位置。但是我的观点太多了。

我想知道我是否可以使用“Key-Value Observing”实现此行为

如果可以,如何实现。

最佳答案

//added KVO for webview
[webview addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionOld context:NULL];

//when observer changed would call:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if([keyPath isEqualToString:@"frame"]) {
//do something
NSLog(@"new:%@",[change objectForKey:NSKeyValueChangeNewKey]);
NSLog(@"old:%@",[change objectForKey:NSKeyValueChangeOldKey]);
}
}

//when you don't need kvo, remove observer
[webview removeObserver:self forKeyPath:@"frame"];

keyPath
The key path, relative to the receiver, of the property to observe. This value must not be nil.

change
A dictionary that describes the changes that have been made to the value of the property at the key path keyPath relative to object.

关于ios - Xcode 如何检测一个 View 的属性改变了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31201583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com