2014-02-18 38 views
0

我正在使用绘制矩形中的以下代码绘制一个简单的实心圆。如何剪辑使用CGContextFillEllipseInRect与另一个圆创建的圆形

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

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

回答

0

我能,使用EO规则做。

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

就像我说的,但你的答案不是那样搞砸了 – peko

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); 
相关问题