Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS, KVO: Observer fails to track change to myObject.myCALayer.center
    primarykey
    data
    text
    <p>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. </p> <p>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?</p> <p>I would like to ' observe ' myObject.myCALayer.center, and every time it changes, send the new value into my touchinput-processing class.</p> <p>so I have done this:</p> <pre><code>- (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!"); &lt;-- this line does not get hit - why? } </code></pre> <p>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?</p> <p>UPDATE: apparently there is some issue with KVO on CALayer properties.</p> <p>I have tried subclassing CALayer, like this:</p> <p>@implementation WatchedLayer</p> <pre><code>+(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; // &lt;-- 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 </code></pre> <p>but still no joy</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload