Sreeram
Sreeram

Reputation: 3258

How to clip the rectangle around the circle when drawing with quartz2d?

I made of subclass of UIView called Bubble using which i want to draw a solid circle.Here is the drawRect: rect method

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // Drawing with a white stroke color
    CGContextSetRGBStrokeColor(ctx, 1.0, 1.0, 1.0, 1.0);
    // And draw with a blue fill color
    CGContextSetRGBFillColor(ctx, 0.0, 0.0, 1.0, 1.0);
    // Draw them with a 2.0 stroke width so they are a bit more visible.
    CGContextSetLineWidth(ctx, 2.0);

    CGRect rrect = CGRectMake(self.bounds.origin.x, self.bounds.origin.y, self.bounds.size.width, self.bounds.size.height);
    CGFloat radius = 30.0;

    CGFloat minx = CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect), maxx = CGRectGetMaxX(rrect);
    CGFloat miny = CGRectGetMinY(rrect), midy = CGRectGetMidY(rrect), maxy = CGRectGetMaxY(rrect);

    // Start at 1
    CGContextMoveToPoint(ctx, minx, midy);
    // Add an arc through 2 to 3
    CGContextAddArcToPoint(ctx, minx, miny, midx, miny, radius);
    // Add an arc through 4 to 5
    CGContextAddArcToPoint(ctx, maxx, miny, maxx, midy, radius);
    // Add an arc through 6 to 7
    CGContextAddArcToPoint(ctx, maxx, maxy, midx, maxy, radius);
    // Add an arc through 8 to 9
    CGContextAddArcToPoint(ctx, minx, maxy, minx, midy, radius);
    // Close the path
    CGContextClosePath(ctx);
    // Fill & stroke the path
    CGContextDrawPath(ctx, kCGPathFillStroke);
}

I have taken the code from the apple sample Quartzdemo.

My problem is i am drawing circle inside a rectangle,but still i see the corners of the rectangle drawn.

How to clip them so that i see only circle? please help. i am new to quartz 2d

Here is how i call it in my viewDidLoad of my controller

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.5];
    Bubble *bub = [[Bubble alloc]initWithFrame:CGRectMake(210.0, 90.0, 60.0, 60.0)];
    [self.view addSubview:bub];
    [bub release];
}

Upvotes: 0

Views: 697

Answers (1)

Vinnie
Vinnie

Reputation: 1740

Put this is viewDidLoad

bub.backgroundColor = [UIColor clearColor];

Upvotes: 1

Related Questions