Search code examples
cocoacalayernsview

cocoa: index for CALayer


I am doing some advanced drawing in my NSView subclass using CALayers using -(void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx In this callback I am given a CALayer, but I need to know the z-index of that layer in an arbitrarily large list of layers (all of which are sublayers of the root layer).

I tried int index = [self.layer.sublayers indexOfObject:someCALayer]; but this does not seem to work.

How can I determine the index of a CALayer?


Solution

  • I ended up setting the z-index property of each CALayer as I added them, and used that to determine position.

    for (int x = 0; x < NUM_LAYERS; x++) {
       CALayer* layer = [[CALayer alloc] init];
       // initialize stuff
       layer.zPosition = x;
       [self.layer addSublayer:layer];
    }