I’m trying to add a shadow behind a circular view, but my attempts have led me to only a shadow on my view’s border—and this shadow doesn’t appear all the way around the view either (just the top). Here’s my code:
-(void)drawRect:(CGRect)dirtyRect{
CGContextRef ctx=UIGraphicsGetCurrentContext();
CGRect bounds=[self bounds];
// Figure out the centre of the bounds rectangle
CGPoint centre;
centre.x=bounds.origin.x+0.5*bounds.size.width;
centre.y=bounds.origin.y+0.5*bounds.size.height;
// Clip context
CGPathRef path = CGPathCreateWithEllipseInRect(bounds, NULL);
CGContextAddPath(ctx, path);
CGContextClip(ctx);
// Add black outline
path = CGPathCreateWithEllipseInRect(bounds, NULL);
CGContextAddPath(ctx, path);
[[UIColor blackColor] setStroke];
CGContextSetLineWidth(ctx, 3.0);
// Specify shadow
CGSize offset=CGSizeMake(1,4);
CGColorRef colour=[[UIColor darkGrayColor] CGColor];
CGContextSetShadowWithColor(ctx, offset, 2, colour);
// Draw image
UIImage *littleImage=[UIImage imageNamed:@"image.png"];
[littleImage drawInRect:bounds];
CGContextStrokePath(ctx);
}
Thanks for reading.
OK, there were a couple things that needed doing to fix this. I think that this code could likely be reduced by a few lines, but here’s what worked: