【发布时间】:2013-07-04 08:28:30
【问题描述】:
我正在尝试使用核心图形从矩形中删除圆形。这样我就可以透过圆孔(像舷窗一样)看到
我已经广泛搜索并尝试利用此处提供的答案Core Graphics, how to draw a Rectangle with an ellipse transparency hole?,但我无法让它发挥作用。我所做的只是在矩形顶部画一个圆圈。这是我最终得到的代码,感谢您的帮助
- (void)drawRect:(CGRect)rect{
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
CGMutablePathRef cutoutRect = CGPathCreateMutable();
CGPathAddRect(cutoutRect, NULL, rect);
CGPathAddEllipseInRect(cutoutRect, NULL, CGRectMake(self.bounds.size.width / 4, self.bounds.size.height / 4, self.bounds.size.width / 2, self.bounds.size.width / 2));
CGContextAddPath(context, cutoutRect);
CGContextSetRGBFillColor(context, 1.0, 1.0, 0.0, 1.0);
CGContextEOFillPath(context);
//Remove the elipse
CGContextEOClip(context);
}
【问题讨论】:
标签: iphone core-graphics quartz-core