我有下面的代码,它应该绘制一个描边和填充的矩形,但填充不会显示。
[[UIColor greenColor] setStroke];
[[UIColor brownColor] setFill];
CGContextBeginPath(context);
CGContextMoveToPoint(context, right, bottom);
CGContextAddLineToPoint(context, right,top);
CGContextAddLineToPoint(context,left, top);
CGContextAddLineToPoint(context, left, bottom);
CGContextAddLineToPoint(context, right, bottom);
CGContextStrokePath(context);
CGContextFillPath(context);
描边有效,我得到了一个漂亮的绿色矩形,没有填充(或白色填充)。这是在 iOS 的 UIView 中。看起来很简单,让我抓狂!
最佳答案
正确的做法是将绘图模式设置为同时包含填充和路径。
CGPathDrawingMode mode = kCGPathFillStroke;
CGContextClosePath( context ); //ensure path is closed, not necessary if you know it is
CGContextDrawPath( context, mode );
您可以使用 CGContextFillPath()
来简单地进行填充,但它基本上只是自动调用 CGContextClosePath()
的 CGContextDrawPath()
和使用 kCGPathFill
作为 CGPathDrawingMode
。您不妨始终使用 CGContextDrawPath()
并输入您自己的参数以获得所需的填充/描边类型。您还可以使用其中一种 Even-Odd 绘图模式在凸路径中放置孔。
关于objective-c - Objective C 中的 CGContextFillPath 如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10553776/