Question

I am a newbie in iOS development. I have two bezier curves which are oval in shape.

UIBezierPath *bpath1 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(x, y, 40, 40)];

    UIBezierPath *bpath2 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(x + 10, y + 10, 20, 20)];

I want to fill a colour between these to ovals. To get a white colour in the smaller oval I tried this

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    UIBezierPath *bpath1 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(x, y, 40, 40)];

    UIBezierPath *bpath2 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(x + 10, y + 10, 20, 20)];

    UIColor *fillColor1 = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.5];
    UIColor *fillColor2 = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];

    CGContextSetStrokeColorWithColor(context,[UIColor blackColor].CGColor);
    CGContextSetLineWidth(context, 1.2);

    CGContextAddPath(context, bpath1.CGPath);
    [fillColor1 setFill];
    [bpath1 fill];        

    CGContextAddPath(context, bpath2.CGPath);
    [fillColor2 setFill];
    [bpath2 fill];
    CGContextStrokePath(context);
}

But the colour of outer circle gets filled in the inner one. Is there any way to only fill the path between these two ovals?

Was it helpful?

Solution

If you are just trying to fill the smaller oval with a white colour, change your fillColor2 code to this:

UIColor *fillColor2 = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];

or this:

UIColor *fillColor2 = [UIColor whiteColor];

The smaller circle is showing up black because filling red, green, and blue values with 0 results in black.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top