views:

196

answers:

2

I have a simple circle drawn in my sub classed uiview as below. How do I add a slight drowshadow on the bottom of the circe?

 - (void)drawRect:(CGRect)rect
   {
     CGContextRef ctx = UIGraphicsGetCurrentContext();
     UIGraphicsPushContext(ctx);
     CGContextSetRGBFillColor(ctx, 1.0f, 1.0f, 1.0f, 1.0f);  // white color
     CGContextFillEllipseInRect(ctx, CGRectMake(10.0f, 10.0f, 100.0f, 100.0f));  // a white filled circle with a diameter of 100 pixels, centered in (60, 60)
     UIGraphicsPopContext();

     //Now what here?
   }
+1  A: 

See Quartz2D Shadows guide.

CGContextSetShadowWithColor (myContext, myShadowOffset, 5, myColor);
slf
+1  A: 

To follow on slf's answer, you'd replace the code above with:

- (void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    UIGraphicsPushContext(ctx);
    CGContextSetRGBFillColor(ctx, 1.0f, 1.0f, 1.0f, 1.0f);  // white color
    CGContextSetShadow(ctx, CGSizeMake(2.0f, 2.0f), 2.0f);
    CGContextFillEllipseInRect(ctx, CGRectMake(10.0f, 10.0f, 100.0f, 100.0f));  // a white filled circle with a diameter of 100 pixels, centered in (60, 60)
    UIGraphicsPopContext();
}

This will create a shadow that is offset by 2 pixels down and to the right of your circle, with a blur of 2 pixels. You can alter these values to create the effect you want. CGContextSetShadowWithColor() can also be used with a different color than black, if you want to add a glow effect to this drawing.

Brad Larson