Search code examples
iosipadcore-graphicsquartz-2d

Drawing a circle ,filled different parts with different color


I am a novice ios programmer.In one of my project i need to draw a circle in which different portion of the circle would be filled up with different colors.I can draw the circle.But i am not being able to determine the different portion of the circle and fill them with different color.Here is an screenshot to clarify what i want to draw.circle with different color

A help would be appreciated.


Solution

  • You can use UIBezierPath which has a method addArcWithCenter:radius:startAngle:endAngle:clockwise: where you can specify radius, center and angles. The code could look like this which draws a quarter of a circle in green:

    CGPoint center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f, CGRectGetHeight(self.bounds) / 2.f);
    CGFloat radius = center.x - 10.f;
    
    UIBezierPath *portionPath = [UIBezierPath bezierPath];
    [portionPath moveToPoint:center];
    [portionPath addArcWithCenter:center radius:radius startAngle:0.f endAngle:M_PI_2 clockwise:YES];
    [portionPath closePath];
    
    [[UIColor greenColor] setFill];
    [portionPath fill];
    
    UIBezierPath *portionPath1 = [UIBezierPath bezierPath];
    [portionPath1 moveToPoint:center];
    [portionPath1 addArcWithCenter:center radius:radius startAngle:M_PI_2 endAngle:M_PI clockwise:YES];
    [portionPath1 closePath];
    
    [[UIColor blueColor] setFill];
    [portionPath1 fill];
    

    Of course you can also consider to use a library like CorePlot.