如何用石英2d绘制时围绕圆圈剪裁矩形?

时间:2012-02-11 03:29:09

标签: ios quartz-graphics

我用UIView的子类做了一个名为Bubble的子类,我想绘制一个实心的圆圈。这是drawRect: rect方法

- (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);
}

我从Apple样本Quartzdemo中获取了代码。

我的问题是我在一个矩形内绘制圆圈,但我仍然看到绘制了矩形的角落。

如何剪辑它们以便我只看到圆圈?请帮忙。我是石英2d的新手

以下是我在我的控制器viewDidLoad中调用它的方法

- (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];
}

1 个答案:

答案 0 :(得分:1)

把它作为viewDidLoad

bub.backgroundColor = [UIColor clearColor];