我正在尝试监视 UIView 框架原点的变化值并对其做出反应。
我的代码:
[cell.bottomView addObserver:self forKeyPath:@"frame.origin" options:NSKeyValueObservingOptionNew context:NULL];
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"%@ Changed", keyPath);
}
我收到以下我不明白的错误消息:
An instance 0x7587d10 of class NSConcreteValue was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info:
<NSKeyValueObservationInfo 0x7587fd0> (
<NSKeyValueObservance 0x7587f70: Observer: 0x7587bd0, Key path: origin, Options: <New: YES, Old: NO, Prior: NO> Context: 0x0, Property: 0x7587ff0>
)
实际上,我只对原点的 y 位置感兴趣,但如果我将 keyPath 设置为“frame.origin.y”,代码甚至无法编译。
如果我将 keyPath 设置为简单的“frame”,则不会出现错误,但永远不会调用observeValueForKeyPath 方法。我想对框架原点的更改不算对框架的更改..?
如何完成将观察者添加到 UIView 的 frame.origin.y ?
虽然我无法完全按照我最初想要的方式解决我的问题,但我发现我能够将观察者正确添加到 UIView 的中心。
通过监视我的视图中心,我可以确定框架的原点已更改。
[cell.bottomView addObserver:self forKeyPath:@"center" options:NSKeyValueObservingOptionNew context:NULL];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)