You could create the shadow using Core Graphics. The building blocks you'll need are described in QuartzDemo sample. In particular have a look at class QuartzMaskingView
in QuartzClipping.m.
- Capture the content of the shape layer into image
- Set the shadow to your liking
- Begin transparency layer
- Clip to the image of layers content - you'll be drawing outside of it
- Draw your image again
This results in shadow being painted outside of your masked area.
CGSize size = CGSizeMake(300, 100);
UIGraphicsBeginImageContextWithOptions(size,NO, 0.0);
[shapeLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect flippedImageRect =
CGRectMake(0, 0, image.size.width, -image.size.height);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGContextSetShadowWithColor(ctx, CGSizeMake(4, 4), 2,
[[UIColor colorWithWhite:0 alpha:0.4] CGColor]);
CGContextBeginTransparencyLayer(ctx, NULL);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextClipToMask(ctx, flippedImageRect, [image CGImage]);
CGContextSetFillColorWithColor(ctx, [[UIColor redColor] CGColor]);
CGContextDrawImage(ctx, flippedImageRect, [image CGImage]);
CGContextEndTransparencyLayer(ctx);
CGContextRestoreGState(ctx);