views:

805

answers:

1

I'm using this code in my UIView subclass to draw a circle with a gradient fill:

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetShadow (context, CGSizeMake(4,4), 5);
    CGContextBeginPath (context);
    CGContextAddEllipseInRect(context, CGRectMake(self.bounds.origin.x, self.bounds.origin.y, 38, 38));
    CGContextClosePath (context);
    CGContextClip (context);
    CGContextDrawLinearGradient(context, gradient, CGPointMake(CGRectGetMinX(self.bounds), CGRectGetMaxX(self.bounds)), CGPointMake(CGRectGetMaxX(self.bounds), CGRectGetMinY(self.bounds)), 0);
}

The circle and the gradient draw fine, but I see no shadow. I'm not sure why its not working, because I used the same CGContextSetShadow function in a different view subclass and it worked fine.

NOTE: In the above code, "gradient" is an ivar that was defined previously.

+1  A: 

A gradient draw doesn't count as a fill; only fills and strokes get shadows. (You may want to report this as a bug.)

As a workaround, fill the path (with solid black), then turn off the shadow, then draw the gradient.

Peter Hosey
Did you mean turn on or turn off the shadow? I'm not quite getting the train of thought.
macatomy
Turn off. Fill with it on, then turn it off and draw the gradient with it off.
Peter Hosey