Alan
Alan

Reputation: 796

Drawing a transparent circle with Core Graphics

I am trying to draw a transparent circle on the screen, where the screen has a black background with 0.5 alpha. However my transparent circle is not clearing the black background. How could I do that? If I wanted a transparent rectangle I could use CGContextClearRect(context,rect) and it clears the background. Any ideas for a circle?

CGContextRef context = UIGraphicsGetCurrentContext();

// Fill the screen with black 0.5 alpha color
CGContextSetRGBFillColor(context,0., 0., 0., 0.5);
CGContextFillRect(context, CGRectMake(0.0, 0.0, 320, CGRectGetHeight(self.frame)));

CGRect circleFrame = CGRectMake(0, 0, 320, 320);

// Draw a circle with a transparent fill
CGContextSetFillColor(context, CGColorGetComponents([UIColor clearColor].CGColor));
CGContextAddEllipseInRect(context, circleFrame);
CGContextFillPath(context);

Upvotes: 1

Views: 1212

Answers (1)

hatfinch
hatfinch

Reputation: 3095

First set up the path of your circle, then call CGContextClip(), then draw your background.

Upvotes: 1

Related Questions