2011-12-09 81 views
1

的我画椭圆:iPhone如何半夹椭圆

CGContextFillEllipseInRect(contextRef, CGRectMake(50, 50, 50, 128)); 

但我只需要椭圆形的一半,是有办法夹的另一半?

回答

5

在调用绘图方法,你可以剪辑的背景下,以椭圆的一部分:那解决我的问题的一半

CGContextSaveGState(contextRef); 
BOOL onlyDrawTopHalf = YES; 
CGFloat halfMultiplier = onlyDrawTopHalf ? -1.0 : 1.0; 
CGRect ellipse = CGRectMake(50, 50, 50, 128); 
CGRect clipRect = CGRectOffset(ellipse, 0, halfMultiplier * ellipse.size.height/2); 
CGContextClipToRect(contextRef, clipRect); 
CGContextFillEllipseInRect(contextRef, ellipse); 
// restore the context: removes the clipping 
CGContextRestoreGState(contextRef); 
+0

。现在另一方面,我想也有半椭圆(与第一个椭圆不同),但再次使用您的代码并不会起作用,新的椭圆从不显示。我只是将BOOL更改为NO,以及椭圆的一些参数。 – MegaManX

+0

你用NO运行代码时看到了什么? – FelixLam

+0

我已经更新了代码。它现在在绘制后恢复上下文的状态。这有帮助吗? – FelixLam