iphone - 在 CGPath 之间着色

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

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

最佳答案

您需要使用 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/

相关文章:

iphone - Airplay样本音频

iphone - 如何在不将值作为子项添加到场景的情况下更新值

ios - UINavigationBar 在屏幕截图中显示为灰色

iphone - 将某些事情委托(delegate)给操作系统

iOS:如何要求应用程序仅在 4"显示器上运行

iphone - 带有 UIImage imageWithContentsOfFile 的 block 状图像

ios - Swift 协议(protocol)作为从 Objective C 类调用的初始化参数

iphone - 如何在动画过程中找到CAlayer的位置?

ios - 如何将某个单元格放在前面

ios - UI 元素在 UIScrollView swift 中不起作用