简体   繁体   English

iOS:将观察者添加到UIView的frame.origin.y?

[英]iOS: Adding observer to a UIView's frame.origin.y?

I'm trying to monitor and react to the changing value of the origin of a UIView's frame. 我正在试图监视和回应UIView框架原点的变化值。 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>
)

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". 真的,我只对原点的y位置感兴趣,但是如果我将keyPath设置为“frame.origin.y”,代码甚至都不会编译。

If I set the keyPath to simply "frame", there are no errors but the method observeValueForKeyPath never gets called. 如果我将keyPath设置为简单的“frame”,则没有错误,但方法observeValueForKeyPath永远不会被调用。 I guess a change to a frame's origin doesn't count as a change to the frame..? 我猜对帧起源的改变不算作对帧的改变..?

How do I accomplish adding an observer to a UIView's frame.origin.y ? 如何完成向UIView的frame.origin.y添加观察者?

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. 虽然我无法以我最初想要的方式完全解决我的问题,但我发现我能够正确地将观察者添加到UIView的中心。 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];

With ReactiveCocoa is as simple as: 使用ReactiveCocoa非常简单:

[RACObserve(self.view, frame) subscribeNext:^(NSNumber *rect) {
        NSLog(@"position y: %f", rect.CGRectValue.origin.y);
    }];

for example. 例如。

I don't think it is possible observe only the y value of the frame. 我认为不可能只观察框架的y值。 But instead of observing the frame property you could overwrite the setFrame: method 但是,您可以覆盖setFrame:方法而不是观察frame属性

Like this: 像这样:

- (void)setFrame:(CGRect)frame 
{
    [super setFrame:frame]
    if (self.frame.origin.y != frame.origin.y){
         // Do your stuff here
    }
 }

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

相关问题 当我改变UIVIew的bounds.size.height时,frame.origin.y会改变 - the frame.origin.y change on when I change UIVIew's bounds.size.height 根据他们的frame.origin.y对UIView的NSMutableArray进行排序 - Sort a NSMutableArray of UIView according to their frame.origin.y UIView:alpha:0,隐藏:YES,removeViewFromSuperview和frame.origin.y = -100000之间的性能/内存差异; - UIView: Performance/memory difference between alpha:0, hidden:YES, removeViewFromSuperview and frame.origin.y = -100000; Imageview frame.origin.x frame.origin.y 总是返回 0,0 - Imageview frame.origin.x frame.origin.y always return 0,0 使用frame.origin.y在滚动视图中查找元素的y位置时遇到麻烦 - Having trouble finding y positions of elements inside scrollview using frame.origin.y UIView的框架origin.y = 0,但显示在origin.y = statusBarHeight - UIView's frame origin.y = 0 but displayed at origin.y = statusBarHeight iOS:UIView的来源y在iOS 9上未更改 - iOS: UIView origin y not changing on iOS 9 更改UIView框架大小不会更改UITableview框架原点y - Changing UIView frame size does not change UITableview frame origin y iOS:UIView关于UIWindow的起源 - iOS: UIView's origin with respect to UIWindow UIAnimation-从button.frame.origin.y显示UIView - UIAnimation - Show UIView from button.frame.origin.y
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM