Add shadow around masked area CALayer

I mask CAGradientLayer with CAShapeLayer to create a bookmark that is inside specific rows in my table.

enter image description here

  CGMutablePathRef path = CGPathCreateMutable(); CGPathAddLines(path, NULL, (CGPoint[]){ CGPointMake(8, 0), CGPointMake(22, 0), CGPointMake(22, 22), CGPointMake(15, 16), CGPointMake(8, 22) }, 5); CGPathCloseSubpath(path); CAShapeLayer *mask = [CAShapeLayer layer]; mask.fillColor = [UIColor redColor].CGColor; mask.bounds = CGRectMake(0, 0, 30, 30); mask.path = path; mask.anchorPoint = CGPointMake(0, 0); CGPathRelease(path); CAGradientLayer *favoriteBadge = [CAGradientLayer layer]; favoriteBadge.colors = @[(id)[UIColor colorWithRed:.97f green:0.5f blue:0.1f alpha:1.f].CGColor, // orange (id)[UIColor colorWithRed:1.f green:.8f blue:0.f alpha:1.f].CGColor]; // yellow favoriteBadge.locations = @[@0.0, @1.0]; favoriteBadge.bounds = CGRectMake(0, 0, 30, 30); favoriteBadge.anchorPoint = CGPointMake(1, 0); favoriteBadge.position = CGPointMake(self.bounds.size.width, 0); favoriteBadge.mask = mask; // FIXME: This shadow is not being applied favoriteBadge.shadowColor = [UIColor colorWithRed:.97f green:0.5f blue:0.1f alpha:1.f].CGColor; favoriteBadge.shadowOpacity = 1.f; 

It looks too flat, so I want to end it with a shadow around the shape itself. I tried to include a shadow on the layer, but it does not seem to apply. I assume that it is applied in an area that is not inside the form. How to add a shadow to the figure itself?

I could copy the layer, darken it and shift its position by a few pixels, but that seems wasteful if CoreAnimation can do it more efficiently for me.

+8
iphone core-animation
source share
2 answers

Create a container layer for the masked layer and apply a shadow to the container layer. I believe this is described here:

http://travisjeffery.com/b/2012/08/ios-how-to-mask-and-shadow-an-image/

+8
source share

Perhaps the only thing you missed is

 favoriteBadge.masksToBounds = NO 
-2
source share

All Articles