我正在尝试绘制一个被椭圆挖空的 320x480 矩形。
想象一下绘制一个填充的矩形,在矩形上绘制一个填充的椭圆,然后从矩形中删除椭圆,留下一个透明的孔。
- (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);
}
当我尝试从矩形中删除日食时,它不起作用。
有人有解决办法吗?
这超出了我的想象,但是...
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
,我永远不能保持笔直. The CGContextClip
应该使用 -type 函数before绘制而不是之后,但在这种情况下它们可能没有必要。
也就是说,你不应该在-drawRect:
实现,但路径应该设置在CAShapeLayer
这是该视图的子层,或其唯一层,或者实际上,如果可能的话,可以代替该视图使用的层。
另请注意,矩形传入drawRect:
可能只是整个视图的一部分,因此您的代码将按原样产生一些非常奇怪的结果。你的意思self.bounds
?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)