简体   繁体   中英

Objective-C: Retrieving CGPoint from Key Value Observing

I'm monitoring the center of a view with the key value observer pattern, as follows:

- (void)viewDidLoad {
    [[self.balanceView viewWithTag:BASE_TAG] addObserver:self
                                              forKeyPath:@"center"
                                                 options:0
                                                 context:BASE_CONTEXT];
}

I'm trying to retrieve the value of the centerpoint and output it to NSLog, as follows:

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (BASE_CONTEXT == context) {
        CGPoint baseCenter = [[change objectForKey:NSKeyValueChangeNewKey] CGPointValue];
        NSLog(@"Base: (%f,%f)", baseCenter.x, baseCenter.y);
    }

}

Whenever the UIView moves, an entry is output to NSLog. However, the centerpoint is always (0,0):

2014-07-18 20:48:51.210 Balance[8646:90b] Object: (0.000000,0.000000)

What is the correct way to retrieve the centerpoint?

options:0不提供更改字典值-如果要“ new”,则应使用1,或更合适的方法是NSKeyValueObservingOptionNew

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM