在CGPath之间着色

时间:2012-08-23 14:49:35

标签: iphone objective-c ios ipad core-graphics

我正在构建一个绘图类型工具到我的应用程序中。

它需要用户的触摸点并在点之间绘制线条。如果用户创建3个或更多触摸点,则它将最后一个点连接到第一个点。

代码的摘录是:

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);

我希望“着色”这些路径中包含的区域。 我该怎么看?

1 个答案:

答案 0 :(得分:1)

您需要使用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);
相关问题