I'm trying to apply a mask to both a base layer and a background layer, but the solutions below blows up when setting the mask on either layer. Could anyone please let me know what I'm doing wrong?
self.backLayer = [[CALayer alloc] init];
[self.backLayer setBounds:[self bounds]];
[self.backLayer setPosition:CGPointMake([self bounds].size.width/2, [self bounds].size.height/2)];
self.backLayer.opacity = 0.3f;
[[self layer] insertSublayer:self.backLayer atIndex:0];
[[self layer] setMasksToBounds:YES];
[[self layer] setBorderWidth:0.0f];
UIBezierPath* path = [UIBezierPath bezierPathWithOvalInRect:[self bounds]];
self.backLayer2 = [CAShapeLayer layer];
self.backLayer2.path = path.CGPath;
self.layer.mask = self.backLayer2;
self.backLayer.mask = self.backLayer2;
CGPathRelease(path.CGPath);