objective-c - UIColor SetFill不起作用

标签 objective-c ios core-graphics uicolor

在这段代码中

for (int i=0;i<3;i++) {
    CGContextAddLineToPoint(context, self.xShift+15+self.rectLen*self.width+self.rectLen*(i+1), self.yShift+self.rectLen*10);
    CGContextAddLineToPoint(context, self.xShift+15+self.rectLen*self.width+self.rectLen*(i+1), self.yShift+self.rectLen*10+self.rectLen);
    CGContextAddLineToPoint(context, self.xShift+15+self.rectLen*self.width+self.rectLen*i, self.yShift+self.rectLen*10+self.rectLen);
    CGContextAddLineToPoint(context, self.xShift+15+self.rectLen*self.width+self.rectLen*i, self.yShift+self.rectLen*10);
    CGContextMoveToPoint(context, self.xShift+15+self.rectLen*self.width+self.rectLen*i, self.yShift+self.rectLen*10);
}
    CGContextAddLineToPoint(context, self.xShift+15+self.rectLen*self.width+self.rectLen*4, self.yShift+self.rectLen*10);
    CGContextAddLineToPoint(context, self.xShift+15+self.rectLen*self.width+self.rectLen*4, self.yShift+self.rectLen*10+self.rectLen);
    CGContextAddLineToPoint(context, self.xShift+15+self.rectLen*self.width+self.rectLen*3, self.yShift+self.rectLen*10+self.rectLen);
    CGContextAddLineToPoint(context, self.xShift+15+self.rectLen*self.width+self.rectLen*3, self.yShift+self.rectLen*10);
    [[UIColor cyanColor] setFill];
    [[UIColor blackColor] setStroke];
    CGContextSetLineWidth(context, 1);
    CGContextDrawPath(context, kCGPathStroke);

使用setFill方法的行不起作用。这可能是什么问题?代码位于drawRect:方法中

最佳答案

setFill不是用于Core Graphics绘图,而是用于[myUIBezierPath fill]之类的绘图;

而是使用以下方法设置填充颜色和笔触颜色:

CGContextSetFillColorWithColor(context, [[UIColor cyanColor] CGColor]);
CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);

另外,以下行:
CGContextDrawPath(context, kCGPathStroke);

由于绘制模式设置为kCGPathStoke,因此只会绘制路径。要同时填充它,您应该将其替换为
CGContextDrawPath(context, kCGPathFillStroke);

如果路径上有孔或自身交叉,则应使用奇偶填充和描边
CGContextDrawPath(context, kCGPathEOFillStroke);

关于objective-c - UIColor SetFill不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10623545/

相关文章:

iPhone - NSDateComponents/NSCalendar 和耗时(例如,3 小时前扫描。)

ios - 应用程序在 tableview 的 reloadData 方法上崩溃

ios - 当在两个 UIViewControllers 之间调用 segue 时,屏幕需要从右向左滑动

ios - 在 UISearchDisplayController 后面重新加载 UITableView

IOS memcpy 纹理到双数组

objective-c - 为什么 SLComposeViewController 不在 Ipad 中共享文本和链接?

ios - 如何使用 NSTimer 传递两个参数?

ios - 从 UITableViewCell 获取 UITableView separatorColor?

objective-c - 如何在 Objective-C 中形成 CGPoint 数组?

ios - 从 CoreGraphics 升级