如何使用Core Graphics/Quartz创建一个填充圆的部分

3
我已经创建了UIView的子类,并试图在我的drawRect方法中绘制部分圆形。我尝试使用bezierPathWithArcCenter来填充,但这只会产生一个像饼一样的形状(图像3),而这不是我想要的结果。也许我可以以某种方式裁剪一个完整的圆?圆周周围的区域需要是透明的。如下图1和2所示。
2个回答

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

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。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接