简体   繁体   English

Xcode如何检测视图的属性已更改?

[英]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. 您可以使用DelegateNSNotificationCenter将值推送到保存其他视图的控制器。

Hope this could help. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 layoutSubviews vs frame didSet用于检测视图的框架何时更改 - layoutSubviews vs frame didSet to detect when the view's frame changed 我的XCode UI测试如何检测屏幕是否已更改? - How can my XCode UI test detect that the screen has changed? ios - 如何在swift ios xcode中检测UIView大小何时更改? - How to detect when UIView size is changed in swift ios xcode? 如何检测哪个方向的地图视图更改了区域 - How to detect in which direction map view changed region 每次在Xcode 4.3中点击按钮标题时,如何更改? - How can a button's title be changed everytime it is tapped in Xcode 4.3? 如何检测UITapGestureRecognizer的单击视图? - How to detect UITapGestureRecognizer 's clicked view? 如何在 Xcode 调试器中查看 RealmSwift 托管对象和属性 - How to view RealmSwift managed objects and properties in Xcode debugger 如何使用XCode的查找工具找到不是IBOUtlets的弱属性? - How to find weak properties that are not IBOUtlets using XCode's find tool? 在Xcode 4.2 SCM功能中查看已更改的文件 - View changed files in Xcode 4.2 SCM features SpriteKit的坐标系在Xcode 8中有变化吗? - Has SpriteKit's coordinate system changed in Xcode 8?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM