views:

28

answers:

1

This might be my lack of understanding of the call stack when using Core Animation, but something confuses me a little about who/what is calling drawInContext.

Lets say I have

@interface PlayerLayer : CALayer 
{
    int Foo;
}

and I initialize Foo in init to be something like

-(void) init
{
  if( self = [super init] ) 
  {
    Foo = 123;
  }
}

When drawInContext is first called, I print out Foo

- (void)drawInContext:(CGContextRef)ctx
{
    NSLog(@"Foo: %d", Foo)
}

The first time 'drawInContext' is called when the layer is added and setNeedsDisplay triggers the call, Foo = 123 as it should.

I have an animation set up for a property key called "Bar" for example, in which 'needsDisplayForKey' returns YES for the key "Bar". When this animation triggers 'drawInContext' all the instance variables are set to 0, and as such Foo prints out as 0. Also the 'self' pointer value has changed.

Can anyone shine any light on why drawInContext seems to be called from some other magical location, and not from within the instance of the layer that I expect it to be?

It means that any state variables that I have stored in the instance cant be used :-(

+1  A: 

When doing an animation, Core Animation makes a copy of the layer and assign's it to the layer's presentationLayer property. The animation is then executed on the presentationLayer. AFAIK only properties are copied, not ivars. Try declaring Foo as a property and see if that helps.

Ole Begemann
Thanks Ole, i thought that may have been the case so I gave it a go.Do you know whet in the documentation I might find information on the copy process?Or, do you have an idea on how to access this data from an animation?
Fuzz
You are right Ole, except that properties dont seem to be copied automatically. Instead the user can override initWithLayer and force custom properties to be copied.See:http://stackoverflow.com/questions/2797103/how-is-the-presentation-layer-of-a-calayer-generated
Fuzz