Search code examples
ioscore-graphicscgcontextquartz-2d

how to clip a circle created using CGContextFillEllipseInRect with another circle


I am drawing a simple filled circle using following code in draw rect.

CGContextRef con = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(con, CGRectMake(0,0,self.bounds.size.width,self.bounds.size.height));
CGContextSetFillColorWithColor(con, [UIColor blueColor].CGColor);
CGContextFillPath(con);

it draws a circle properly. Now I want to clip the circle with another small circle in the middle so that it becomes a hollow circle and you can see whatever is behind the main circle. How can i do that?


Solution

  • I was able to do that using the EO rule.

    CGContextRef con = UIGraphicsGetCurrentContext();
    CGContextAddEllipseInRect(con, rect);
    CGContextAddEllipseInRect(con, CGRectInset(rect, 40, 40));
    CGContextSetFillColorWithColor(con, [UIColor blueColor].CGColor);
    CGContextEOFillPath(con);