Search code examples
iphoneuiviewdropshadow

iphone drow shadow on circle


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?
   }

Solution

  • 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.