Iphone cgp路径之间的着色
我正在我的应用程序中构建一个绘图类型工具 它从用户处获取接触点,并在点之间绘制线。如果用户创建3个或更多接触点,则会将最后一个点连接到第一个点 代码摘录如下:Iphone cgp路径之间的着色,iphone,objective-c,ios,ipad,core-graphics,Iphone,Objective C,Ios,Ipad,Core Graphics,我正在我的应用程序中构建一个绘图类型工具 它从用户处获取接触点,并在点之间绘制线。如果用户创建3个或更多接触点,则会将最后一个点连接到第一个点 代码摘录如下: startPoint = [[secondDotsArray objectAtIndex:i] CGPointValue]; endPoint = [[secondDotsArray objectAtIndex:(i + 1)] CGPointValue]; CGContextAddEllipseInRect(contex
startPoint = [[secondDotsArray objectAtIndex:i] CGPointValue];
endPoint = [[secondDotsArray objectAtIndex:(i + 1)] CGPointValue];
CGContextAddEllipseInRect(context,(CGRectMake ((endPoint.x - 5.7), (endPoint.y - 5.7)
, 9.0, 9.0)));
CGContextDrawPath(context, kCGPathFill);
CGContextMoveToPoint(context, startPoint.x, startPoint.y);
CGContextAddLineToPoint(context, endPoint.x, endPoint.y);
CGContextStrokePath(context);
我希望在这些路径中包含的区域中“着色”。
我应该看什么?您需要使用
CGContextFillPath
API。但是,您应该注意如何定义路径:
- 首先在初始点调用
CGContextMoveToPoint
- 使用
CGContextAddLineToPoint
- 使用
关闭路径。不要将直线添加到最终线段上的点CGContextClosePath
CGContextFillPath
将生成一个使用先前设置的填充颜色着色的路径
以下是一个例子:
CGPoint pt0 = startPoint = [[secondDotsArray objectAtIndex:0] CGPointValue];
CGContextMoveToPoint(context, pt0.x, pt0.y);
for (int i = 1 ; i < noOfDots ; i++) {
CGPoint next = [[secondDotsArray objectAtIndex:i] CGPointValue];
CGContextAddLineToPoint(context, next.x, next.y);
}
CGContextClosePath(context);
CGContextFillPath(context);
CGPoint pt0=startPoint=[[secondDotsArray objectAtIndex:0]CGPointValue];
CGContextMoveToPoint(上下文,pt0.x,pt0.y);
for(int i=1;i
Thank@dasblinkenlight,在我尝试这一点之前,我只是反复检查一下,我的问题问得很清楚。我让iPhone在接触的点之间画蓝线。我要做的是在这三条线之间的区域中着色。这还可以吗?@Ríomhaire如果这三条线代表一条闭合路径(即三角形),那么是的,CGContextFillPath
将在该三角形中着色。我认为我正确地遵循了您的步骤。我的方法有冲突吗?我这里有修改过的方法。干杯@dasbinkenlight@Ríomhaire您做得不对-在绘制线段之间,不应该对上下文进行其他操作。特别是,循环中不能调用CGContextMoveToPoint
,也不能调用CGContextStrokePath
。如果你需要画圆圈什么的,你应该在一个单独的循环中画。有关骨架示例,请参见更新。