我正在我的应用中构建一个绘图类型的工具。
它从用户那里获取触摸点并在点之间画线。如果用户创建了 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);
我希望为这些路径中包含的区域“着色”。 我应该看什么?
最佳答案
您需要使用 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);
关于iphone - 在 CGPath 之间着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12094378/