Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGContextSetShadow produces no results

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.

like image 721
indragie Avatar asked Dec 20 '25 04:12

indragie


1 Answers

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.

like image 130
Peter Hosey Avatar answered Dec 22 '25 19:12

Peter Hosey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!