Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top