【问题标题】:Remove a circle from a rectangle using Core Graphics使用 Core Graphics 从矩形中删除一个圆
【发布时间】: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


    【解决方案1】:

    在使用CGContextEOFillPath 之前,您需要为矩形添加一条路径,为椭圆添加一条路径。这里两种形状混合在一个路径中,这是行不通的。

    顺便说一句,你最后的CGContextEOClip 电话毫无用处。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-11
      • 1970-01-01
      相关资源
      最近更新 更多