Skip navigation.
 
mlRe: CALayer Lookup by Name
FROM : Scott Stevenson
DATE : Sun May 04 07:18:21 2008

On May 3, 2008, at 6:14 PM, Matt James wrote:

> I've been doing a bit of Core Animation recently and have been 
> starting to
> notice the trickery involved with accessing a layer.  It seems as 
> though the
> only way to get at one is to save it to a local variable on an 
> object for
> later retrieval.


Pretty much, yes.

> Am I missing something obvious or does CALayer not have a method for 
> looking up
> and returning a sublayer by name?  If not, sounds like a perfect 
> place to
> put a category!


You're not missing anything obvious. The lack of a "layerForName:" 
might have something to do with the fact that layer names aren't 
necessarily unique. The easiest way to do what you want is to just 
write a category called -firstLayerNamed: which loops through 
sublayers until it finds one.

CALayers are generic KVC containers so you could also do this:


CALayer* parent = [CALayer layer];
CALayer* child = [CALayer layer];
CALayer* grandchild = [CALayer layer];

[parent addSublayer:child];
[parent setValue:child forKey:@"child"];

[child addSublayer:grandchild];
[child setValue:grandchild forKey:@"grandchild"];    

NSLog(@"layer: %@", [parent valueForKeyPath:@"child.grandchild"]);


      - Scott

Related mailsAuthorDate
mlCALayer Lookup by Name Matt James May 4, 03:14
mlRe: CALayer Lookup by Name Scott Stevenson May 4, 07:18