0

Consider the following...

Say I have two CALayer's, one on top of the other. Each layer is the size of the entire iPad screen, the top layer obscures the bottom layer.

Is there some way to mark a portion of the top layer as being "transparent", so that the same section of the bottom layer shows through the transparent portion? In other words, is there a way to "cut out" a portion of the top layer to reveal the bottom layer underneath?

hashier
  • 4,670
  • 1
  • 28
  • 41
Shaun
  • 33
  • 2
  • 7
  • There is a link below that could resolve this issue, http://stackoverflow.com/questions/16512761/calayer-with-transparent-hole-in-it – Lei Zhang Mar 01 '16 at 16:20

4 Answers4

0

Yes, you can do it by making different alpha values for each layer, basically the inner layer(super) one should have at least an alpha value of 0.7 and the outer layer(subLayer) should have less alpha value than its parent, lets say 0.3 Then the outer layer should reveal the inner layer.

But if you wanna do some better revelation, you could draw the outer layer, by setting radial gradient on it.

This is my sample code, but it I haven't drawn the radial gradient for the outer layer.

  // 
        CALayer *innnerLayer = [CALayer layer];
        innnerLayer.borderColor = [UIColor greenColor].CGColor;
        innnerLayer.borderWidth = 0.8f;
        innnerLayer.backgroundColor = [UIColor colorWithWhite:0. alpha:0.5].CGColor;
        innnerLayer.frame = CGRectMake(70.0, 150.0f, 100.0f, 100.0f); 
       CALayer *outLayer = [CALayer layer];
        outLayer.frame = innnerLayer.bounds;
        outLayer.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.3f].CGColor;
        // add outer layer to inner layer
        [innnerLayer addSublayer:outLayer];
         // add the inner layer to main view
        [self.view.layer addSublayer:innnerLayer];

// Experiment with different alpha values, but the outerAlpha

abdimuna
  • 775
  • 9
  • 16
0

Are you using CALayer as a sublayer of your UIView? You have to set the backgroundColor of your UIView to clear like this:

self.backgroundColor = [UIColor clearColor];
hashier
  • 4,670
  • 1
  • 28
  • 41
0

The CALayer mask property. You'll need to subclass CALayer to drawToContext: opaque black over the entire bounds, then do a CGContextClear(ctx, <your see-through box>);

Then create an instance of the layer, give it the same frame as your top-layer bounds, and set it to the mask property.

joerick
  • 16,078
  • 4
  • 53
  • 57
-1

Just setting backgroundColor property to NULL helped me.