Search code examples
iospositioncalayerkey-value-observing

iOS, KVO: Observer fails to track change to myObject.myCALayer.center


I have a view. this view has a wheel in the middle. it also supports iAds and auto rotation. so whenever an ad pops in the top, or the device rotates, the wheel automatically repositions itself to the centre of the open space.

Now I have a class to detect if the user is spinning the wheel round. it is derived from UIAdvancedGesture, but in order for it to work correctly it needs to know the centre of the wheel. Which keeps changing. how to fix?

I would like to ' observe ' myObject.myCALayer.center, and every time it changes, send the new value into my touchinput-processing class.

so I have done this:

- (id) initWithFrame: (CGRect) theFrame
              target: (id) p_target
     actionPlayChord: (SEL) p_actionPlayChord
      actionSettings: (SEL) p_actionSettingsClick
{
    target = p_target;
    actionPlayChord = p_actionPlayChord;

    self = [super initWithFrame: theFrame];

    if ( ! self )
        return nil;

    CGPoint centre = CGPointMake(theFrame.size.width / 2.0, theFrame.size.height / 2.0);

    WheelTouchHandler * wheelTouchHandler = [WheelTouchHandler alloc];
    [wheelTouchHandler initWithTarget: self 
                       activateAction: @selector(engaged:)
                         moveCWAction: @selector(movedCW:)
                        moveACWAction: @selector(movedACW:)
     ] ;

    [self addGestureRecognizer: wheelTouchHandler];

    [self setMultipleTouchEnabled: NO];


    self.wheel = [Wheel alloc];

    NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults];
    NSString *symbol = [prefs stringForKey: @"sharpsOrFlats"];

    [self.wheel init_rMax: WHEEL_R_MAX
                     rMin: WHEEL_R_MIN
                   rGap01: WHEEL_R_RATIO_GAP_TO_DISC
                   rRatio: WHEEL_R_RATIO_OUTER_TO_INNER
              sharpsFlats: symbol
               bitmapSize: theFrame.size 
     ] ;

    [self.layer addSublayer:  [self.wheel wheelLayer] ];

    [self.wheel.wheelLayer setPosition: centre ];


    [self.layer addSublayer:  [self.wheel labelLayer] ];
    [self.wheel.labelLayer setPosition: centre ];

    // "center" fails too...
    [self.wheel.wheelLayer addObserver:self forKeyPath:@"position" options:0 context:nil];


    return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath 
                      ofObject:(id)object 
                        change:(NSDictionary *)change 
                       context:(void *)context
{
    NSLog(@"Changed!"); <--  this line does not get hit - why?
}

However, nothing gets logged. what am I doing wrong? Obviously the wheel's CALayer repositions itself on the screen. but the Observer fails to observe this change. How to fix?

UPDATE: apparently there is some issue with KVO on CALayer properties.

I have tried subclassing CALayer, like this:

@implementation WatchedLayer

+(BOOL) automaticallyNotifiesObserversForKey: (NSString *) key
{

    //, if key is @"position" return YES, otherwise return [super automatically...]

    if ([key isEqual: @"position"])
        return YES; // doesn't get hit

    if ([key isEqual: @"center"])
        return YES; // <-- gets hit

    return [super automaticallyNotifiesObserversForKey: key];
}

- (void) setValue: (id) value 
           forKey: (NSString *) key
{
    // method doesn't get hit


    if ([key isEqual: @"position"])
    {
        NSLog(@"POSHIT");
    }

    [super setValue: value 
             forKey: key];
}

@end

but still no joy


Solution

  • Some of the CALayer properties are not KVO compliant. The frame property, for example, for CALayer's appears to be one of those properties. I've successfully observed changes to the bounds property (CGRect) instead which is KVO compliant (at least in 10.6).

    I've also tested the position property and it is KVO compliant on 10.6. I'm not sure about 10.5 and iOS though.

    It seems there are differences in KVC/KVO compliant properties for CALayer's between 10.5 and 10.6. Specifically, custom properties of CALayer subclasses appear not to be KVO compliant in 10.5, but are compliant in 10.6. There may be other KVO compliance differences between CALayer properties between 10.5, 10.6, and iOS.

    http://lists.apple.com/archives/cocoa-dev/2008/Dec/msg01141.html

    and

    http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreAnimation_guide/Articles/Layers.html%23//apple_ref/doc/uid/TP40006082-SW1