2013-07-04 50 views
0

我正嘗試使用核心圖形從矩形中刪除一個圓形形狀。所以我可以通過圓孔看到(像舷窗)使用Core Graphics從矩形中刪除一個圓圈

我已經搜索了很多,並試圖利用這裏提供的答案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); 
} 

回答

3

使用CGContextEOFillPath你需要添加爲一個矩形路徑和橢圓一條路徑之前。在這裏,兩種形狀混合在一條路徑中,這是行不通的。

而順便說一句,你的最終CGContextEOClip電話是無用的。

+0

謝謝,完美的工作! – peten