objective-c - Objective C 中的 CGContextFillPath 如何工作?

标签 objective-c ios cocoa-touch graphics cgcontext

我有下面的代码,它应该绘制一个描边和填充的矩形,但填充不会显示。

    [[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/

相关文章:

iphone - facebook login给我已经授权这个应用程序而不自动返回到应用程序

ios - 带有参数 iOS 的单例

ios - 我们如何检测并保存 iPhone/iPad 触摸屏上写入的文本?

objective-c - "Block"主线程 (dispatch_get_main_queue()) 和(或不)定期运行 currentRunLoop - 有什么区别?

iphone - 如何在 UITableView 中将 fromIndex 处的 Row 交换为 toIndex?

ios - 为@property 更改调用 UIView#setNeedsDisplay 的正确方法

iphone - 带有 UIPageControl 的 GMGridView

ios - 更改 UISearchController 上的状态栏颜色

iphone - 是否可以阻止 UISearchBar 在 UITableView 中滚动?

ios - 以编程方式创建的 UIView 的中心 X 到另一个 UIView - Objective C