我有以下问题:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineCap(context,kCGLineCapRound);
CGContextSetLineJoin(context, kCGLineJoinRound);
CGContextSetRGBFillColor(context, 1, 0, 0, 1);
for (drawnCurve *line in completeLines) {
CGContextBeginPath(context);
for(int i=0;i<[line.points count]-1;i++)
{
drawnPoint*point=[line.points objectAtIndex:i];
drawnPoint*point2=[line.points objectAtIndex:i+1];
// CGContextSetLineWidth(context, point.r);
[point.color set];
CGContextMoveToPoint(context, point.x,point.y);
CGContextAddLineToPoint(context, point2.x, point2.y);
// CGContextAddCurveToPoint(context, (point.x+point2.x)/2, point2.y, (point.x+point2.x)/2, point2.y, point2.x, point2.y);
//CGContextEOFillPath(context);
}
CGContextClosePath(context);
CGContextFillPath(context);
}
但它只是消失了,没有显示任何填充曲线。问题出在哪里?
对CGContextMoveToPoint
的调用将开始一个新的子路径。您应该在嵌套for
循环之前调用它一次,然后仅使用 CGContextAddLineToPoint
来保留线路连通性:
drawnPoint*pointZero=[line.points objectAtIndex:0];
CGContextMoveToPoint(context, pointZero.x,pointZero.y);
for(int i=1;i<[line.points count];i++)
{
drawnPoint*point=[line.points objectAtIndex:i];
CGContextAddLineToPoint(context, point.x, point.y);
}
CGContextClosePath(context);
CGContextFillPath(context);