如何使用Core Graphics / Quartz创建实心圆的一部分

时间:2012-05-17 08:50:33

标签: iphone ios objective-c core-graphics quartz-graphics

我已经创建了UIView的子类,我试图在drawRect方法中绘制一个圆的一部分。

我已经尝试过使用bezierPathWithArcCenter并填充它,但这只会导致饼形(图片3)并且这不是我想要的。 我想绘制您在图片1和2中看到的内容

也许我可以以某种方式剪辑一个完整的圆圈?圆周围区域需要透明。

Circles

2 个答案:

答案 0 :(得分:3)

TompaLompas的回答指出了我正确的方向(使用弧形绘图部分)。然而,完整的解决方案和答案是这样的:

#define   DEGREES_TO_RADIANS(degrees)  ((M_PI * degrees)/ 180)
- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    int radius = self.frame.size.width / 2;
    CGPoint center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);
//Image 2 
    CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
    CGContextAddArc(ctx, center.x, center.y, radius, DEGREES_TO_RADIANS(225), DEGREES_TO_RADIANS(315), NO);
    CGContextDrawPath(ctx, kCGPathFill);
}

答案 1 :(得分:2)

尝试用这个覆盖drawRect:

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    CGContextRef context = UIGraphicsGetCurrentContext();
    float radius = 50.0f;
    float x_left = rect.origin.x;
    float x_left_center = x_left + radius;
    float y_top = rect.origin.y;
    float y_top_center = y_top + radius;
    /* Begin path */
    CGFloat white[4] = {0.0f, 204.0f/255.0f, 1.0f, 0.8f};
    CGContextSetFillColor(context, white);
    CGContextSetLineWidth(context, 1.0);
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, x_left, y_top_center);
    CGContextAddArcToPoint(context, x_left, y_top, x_left_center, y_top, radius);
    CGContextAddLineToPoint(context,x_left, y_top + radius);

    CGContextFillPath(context);
}

它将绘制旋转的图像编号2