Question

I want to draw a green circle with a white rectangle (or few rectangles) in it. But I can't understand, how to use CGContext methods. Here's my code. Firstly I draw a circle and fill it, then I want to draw something else inside my circle. But if I use same CGContextRef to draw inside primitives, CGContextSetFillColor change the color of my circle.

- (void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextAddEllipseInRect(ctx, rect);
    CGContextSetFillColor(ctx, CGColorGetComponents([self.color CGColor]));
    CGContextFillPath(ctx);

    switch (self.type)
    {
        case ETBRoundViewTypeBook:
        {
            CGRect bookRect = CGRectMake(CGRectGetMidX(rect) - 10, CGRectGetMidY(rect) - 12, 20, 24);
            CGContextAddRect(ctx, bookRect);
            CGContextSetFillColor(ctx, CGColorGetComponents([UIColor whiteColor].CGColor));
            CGContextFillPath(ctx);
        }
        case ETBRoundViewTypeList:
        {
            break;
        }
        case ETBRoundViewTypeTick:
        {
            break;
        }
        default:
            break;
    }
}

EDIT: The problem was solved by replacing CGContextSetFillColor to CGContextSetFillColorWithColor.

Était-ce utile?

La solution

I just tried this and works.

-(void)drawRect:(CGRect)rect{
    CGContextRef ctx =UIGraphicsGetCurrentContext();
    CGContextAddEllipseInRect(ctx, rect);
    CGContextSetFillColorWithColor(ctx, [UIColor greenColor].CGColor);
    CGContextFillPath(ctx);
    CGContextAddEllipseInRect(ctx, CGRectMake(30, 30, 40, 40));
    CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
    CGContextFillPath(ctx);
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top