[英]Xcode how to detect a view's properties changed?
How can I detect a view's x,y,width or height is changed ? 如何检测视图的x,y,宽度或高度是否已更改?
I want to implement that once a view's properties are changed, the other view's positions are reorganised. 我要实现的是,一旦更改了视图的属性,便重新组织了另一个视图的位置。
For example, I have a webview which has dynamic height. 例如,我有一个具有动态高度的Web视图。 Each time webViewDidFinishLoad I will change the height of webView, then the other view's position will be changed according webview.frame.
每次webViewDidFinishLoad时,我都会更改webView的高度,然后根据webview.frame更改另一个视图的位置。
I have been thinking i change all the view's position inside the delegate function webViewDidFinishLoad. 我一直在想我更改委托函数webViewDidFinishLoad中所有视图的位置。 But i have too many views.
但是我有太多意见。
I want to know if I can implement this behaviour using " Key-Value Observing " 我想知道是否可以使用“ 键值观察 ”实现此行为
If i can, how to implement it. 如果可以,如何实施。
//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
keyPath
The key path, relative to the receiver, of the property to observe.要观察的属性相对于接收者的关键路径。 This value must not be nil.
此值不能为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.一个字典,描述相对于对象的键路径keyPath上的属性值所做的更改。
You can use Delegate
or NSNotificationCenter
to push value to controller which is holding other views. 您可以使用
Delegate
或NSNotificationCenter
将值推送到保存其他视图的控制器。
Hope this could help. 希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.