用一种颜色为笔触绘制一个多边形

用一种颜色为笔触绘制一个多边形

本文介绍了用一种颜色为笔触绘制一个多边形,为填充绘制一种不同的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法绘制一些用颜色描边的线条,然后用另一个填充它们的内部(它们形成多边形).

I'm having trouble with drawing some lines that are stroked with a color and then filling their insides (they make a polygon) with another one.

UIColor *houseBorderColor = [UIColor colorWithRed:(170/255.0) green:(138/255.0) blue:(99/255.0) alpha:1];
CGContextSetStrokeColorWithColor(context, houseBorderColor.CGColor);
CGContextSetLineWidth(context, 3);

// Draw the polygon
CGContextMoveToPoint(context, 20, viewHeight-19.5);
CGContextAddLineToPoint(context, 200, viewHeight-19.5); // base
CGContextAddLineToPoint(context, 300, viewHeight-119.5); // right border
CGContextAddLineToPoint(context, 120, viewHeight-119.5);
CGContextAddLineToPoint(context, 20, viewHeight-19.5);

// Fill it
CGContextSetRGBFillColor(context, (248/255.0), (222/255.0), (173/255.0), 1);
//CGContextFillPath(context);

// Stroke it
CGContextStrokePath(context);

CGContextStrokePath 注释掉,我得到这个结果:

With the CGContextStrokePath commented out, I get this result:

但是如果我取消注释 CGContextStrokePath 并填充多边形,颜色会溢出笔划:

But if I uncomment CGContextStrokePath and fill out the polygon, the color overflows the strokes:

你是如何获得这样的结果的(无需重复两次整个绘制过程):

How do you achieve a result like this (without having to redo the whole drawing procedure twice):

推荐答案

您可以使用

CGContextDrawPath(context, kCGPathFillStroke);

代替

CGContextFillPath(context);
CGContextStrokePath(context);

问题是 CGContextFillPath()CGContextStrokePath(context)清除当前路径,这样只有第一个操作成功,第二个操作成功操作什么也没画.CGContextDrawPath() 结合填充和描边清除两者之间的路径.

The problem is that both CGContextFillPath() and CGContextStrokePath(context)clear the current path, so that only the first operation succeeds, and the secondoperation draws nothing. CGContextDrawPath() combines fill and stroke withoutclearing the path in between.

这篇关于用一种颜色为笔触绘制一个多边形,为填充绘制一种不同的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 20:15