How to create part of a filled circle using Core Graphics / Quartz

I created a subclass of UIView, and I'm trying to draw part of a circle in the drawRect method.

I tried to use bezierPathWithArcCenterand fill it, but this will only result in a cake shape (image 3), and this is not what I need. I want to draw what you see in images 1 and 2 .

Maybe I can somehow cut a full circle? The area around the circle should be transparent.

Circles

+3
source share
2 answers

TompaLompas answer pointed me in the right direction (with the arc drawing part). However, the complete solution and answer are as follows:

#define   DEGREES_TO_RADIANS(degrees)  ((M_PI * degrees)/ 180)
- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    int radius = self.frame.size.width / 2;
    CGPoint center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);
//Image 2 
    CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
    CGContextAddArc(ctx, center.x, center.y, radius, DEGREES_TO_RADIANS(225), DEGREES_TO_RADIANS(315), NO);
    CGContextDrawPath(ctx, kCGPathFill);
}
+3
source

try overriding drawRect as follows:

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    CGContextRef context = UIGraphicsGetCurrentContext();
    float radius = 50.0f;
    float x_left = rect.origin.x;
    float x_left_center = x_left + radius;
    float y_top = rect.origin.y;
    float y_top_center = y_top + radius;
    /* Begin path */
    CGFloat white[4] = {0.0f, 204.0f/255.0f, 1.0f, 0.8f};
    CGContextSetFillColor(context, white);
    CGContextSetLineWidth(context, 1.0);
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, x_left, y_top_center);
    CGContextAddArcToPoint(context, x_left, y_top, x_left_center, y_top, radius);
    CGContextAddLineToPoint(context,x_left, y_top + radius);

    CGContextFillPath(context);
}

2

+2

All Articles