核心图形,如何绘制带有椭圆形透明孔的矩形? [英] Core Graphics, how to draw a Rectangle with an ellipse transparency hole?
问题描述
我正在尝试绘制一个用椭圆挖空的320x480矩形。
想象一下绘制一个填充的矩形,在该矩形上填充一个椭圆,然后从该矩形中删除该椭圆,留下一个透明的孔。
-(void)drawRect:(CGRect)rect
{
//绘制代码。
CGContextRef context = UIGraphicsGetCurrentContext();
//将颜色设置为红色
CGContextSetRGBFillColor(context,1.0,0.0,0.0,1.0);
//添加矩形
CGContextAddRect(context,rect);
//填充矩形
CGContextFillRect(context,rect);
//创建一个要从矩形中删除的椭圆
CGPathRef circlePath = CGPathCreateMutable();
CGPathAddEllipseInRect(circlePath,NULL,elipseRect);
CGContextAddPath(context,circlePath);
CGContextSetRGBFillColor(context,1.0,1.0,0.0,1.0);
CGContextFillPath(context);
//剪辑椭圆...(请勿工作)
CGContextEOClip(context);
}
当我尝试从矩形中删除椭圆形时,它不起作用。
有人解决吗?
这不在我的脑海中,但是...
CGPathRef cutoutRect = CGPathCreateMutable();
CGPathAddRect(cutoutRect,NULL,rect);
CGPathAddEllipseInRect(cutoutRect,NULL,ellipseRect);
CGContextAddPath(context,cutoutRect);
CGContextSetRGBFillColor(context,1.0,1.0,0.0,1.0);
CGContextFillPath(context);
您实际上可能需要使用 CGContextEOFillPath
,我绝对不能保持直行。 CGContextClip
类型的函数应该在绘制之前而不是之后使用,但是在这种情况下可能没有必要。 b
$ b
也就是说,您不应该在 -drawRect:
实现中执行此操作,但是应该在 CAShapeLayer
,它是该视图的子层,或者是它的唯一层,或者,实际上,如果可能的话,它是用来代替该视图的层。
还请注意,传递到 drawRect:
的矩形可能只是整个视图的一部分,因此您的代码将按原样具有一些非常奇怪的结果。您是说 self.bounds
吗?
I am trying to draw a 320x480 rectangle that is hollowed by a ellipse. Imagine drawing a filled rectangle, over the rectangle an filled ellipse, and remove the ellipse from the rectangle, leaving a transparent hole.
- (void)drawRect:(CGRect)rect
{
// Drawing code.
CGContextRef context = UIGraphicsGetCurrentContext();
// Set color to red
CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
// Add rectange
CGContextAddRect(context, rect);
// Fill rectange
CGContextFillRect(context, rect);
// Create a elipse to be removed from rectange
CGPathRef circlePath = CGPathCreateMutable();
CGPathAddEllipseInRect(circlePath , NULL , elipseRect);
CGContextAddPath(context, circlePath);
CGContextSetRGBFillColor(context, 1.0, 1.0, 0.0, 1.0);
CGContextFillPath(context);
// clip elipse... (DO NOT WORK)
CGContextEOClip(context);
}
When I am trying to remove the elipse from the rectange, it does not work.
Anybody has a solution?
This is off the top of my head, but...
CGPathRef cutoutRect = CGPathCreateMutable();
CGPathAddRect(cutoutRect, NULL, rect);
CGPathAddEllipseInRect(cutoutRect, NULL, ellipseRect);
CGContextAddPath(context, cutoutRect);
CGContextSetRGBFillColor(context, 1.0, 1.0, 0.0, 1.0);
CGContextFillPath(context);
You may actually need to use CGContextEOFillPath
, I can never keep them straight. The CGContextClip
-type functions should be used before drawing rather than after, but they're probably not necessary in this case.
That said, you shouldn't be doing this in a -drawRect:
implementation, but the path should be set on a CAShapeLayer
which is a sublayer of this view, or its only layer, or, indeed, a layer which is used instead of this view if possible.
Also note that the rectangle passed into drawRect:
may be only part of the whole view, so your code will have some pretty weird results as-is. Did you mean self.bounds
?
这篇关于核心图形,如何绘制带有椭圆形透明孔的矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!