iOS:将观察者添加到UIView的frame.origin.y? [英] iOS: Adding observer to a UIView's frame.origin.y?

查看:460
本文介绍了iOS:将观察者添加到UIView的frame.origin.y?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试监视并对UIView框架原点的变化值做出反应。
我的代码:

I'm trying to monitor and react to the changing value of the origin of a UIView's frame. My code:

[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);
}

我收到以下错误消息,我不明白:

I'm getting the following error message that I don't understand:

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。

Really, I'm only interested in the y position of the origin but the code won't even compile if I set the keyPath to "frame.origin.y".

如果我将keyPath设置为简单的frame,则没有错误,但方法observeValueForKeyPath永远不会被调用。我猜对帧起源的更改不算作对帧的更改。?

If I set the keyPath to simply "frame", there are no errors but the method observeValueForKeyPath never gets called. I guess a change to a frame's origin doesn't count as a change to the frame..?

如何完成向UIView的frame.origin添加观察者。 y?

How do I accomplish adding an observer to a UIView's frame.origin.y ?

推荐答案

虽然我无法完全按照我最初想要的方式解决问题,但我发现我是能够正确地将观察者添加到UIView的中心。
通过监视我的视图中心,我可以确定框架的原点已经改变。

Although I could not solve my problem exactly in the way that I initially wanted, I have found out that I am able to add observers correctly to the UIView's center. By monitoring the center of my view I can determine that the frame's origin has changed.

[cell.bottomView addObserver:self forKeyPath:@"center" options:NSKeyValueObservingOptionNew context:NULL];

这篇关于iOS:将观察者添加到UIView的frame.origin.y?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆