如何将使用CGContextFillEllipseInRect创建的圆圈与另一个圆圈一起剪辑

时间:2014-02-18 12:50:39

标签: ios core-graphics cgcontext quartz-2d

我在draw rect中使用以下代码绘制一个简单的实心圆。

CGContextRef con = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(con, CGRectMake(0,0,self.bounds.size.width,self.bounds.size.height));
CGContextSetFillColorWithColor(con, [UIColor blueColor].CGColor);
CGContextFillPath(con);

它正确地绘制了一个圆圈。现在我想用中间的另一个小圆圈剪切圆圈,使它变成一个空心圆圈,你可以看到主圆圈后面的任何东西。我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

在CGContextEOClip()

中使用偶数规则
CGSize size = rect.size;
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextTranslateCTM(context, 0, 0);
CGContextSaveGState(context);

CGContextAddPath(context, ([UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, size.width, size.height)].CGPath));
CGContextAddPath(context, ([UIBezierPath bezierPathWithOvalInRect:CGRectMake(size.width/4, size.height/4, size.width/2, size.height/2)].CGPath));
CGContextEOClip(context); //clip
CGContextAddPath(context, [UIBezierPath bezierPathWithRect:rect].CGPath);

CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
CGContextFillPath(context);

CGContextRestoreGState(context);

答案 1 :(得分:0)

我能够使用EO规则来做到这一点。

CGContextRef con = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(con, rect);
CGContextAddEllipseInRect(con, CGRectInset(rect, 40, 40));
CGContextSetFillColorWithColor(con, [UIColor blueColor].CGColor);
CGContextEOFillPath(con);