iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 如何填充Quartz 2D中路径下方的区域?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在UIView对象中使用Quartz 2D来绘制几条曲线,如下例所示:

这是我现在的代码(我省略了控制点和其他东西的计算):

for (int i = 1; i < points.count; ++i) {
  CGContextMoveToPoint(context,prevIoUsXValue,prevIoUsYvalue);
  CGContextAddCurveToPoint(context,firstControl@R_352_7616@Value,firstControlpointyValue,secondControl@R_352_7616@Value,secondControlpointyValue,xValue,yValue
  );

  // CGContextFillPath(context);
  CGContextstrokePath(context);
}

现在我想填充曲线下方的区域,但如果我使用CGContextFillPath,结果如下:

这是有道理的,因为根据Quartz 2D documentation

然后我尝试将路径移动到右下角并关闭路径,但填充方法没有任何影响:

CGContextMoveToPoint(context,rect.size.width,rect.size.height);
CGContextClosePath(context);
CGContextFillPath(context);

我怎样才能填满曲线下面的整个区域而不仅仅是在每个子路径中关闭的子区域?

编辑:

我找到了一个临时解决方案:使用曲线绘制形状,并在每个子路径上绘制两条垂直线:

for (int i = 1; i < points.count; ++i) {
  // Draw the curve
  CGContextMoveToPoint(context,yValue
  );
  CGContextstrokePath(context);

  // Draw a shape using curve's initial and final points
  CGContextMoveToPoint(context,rect.size.height);
  CGContextAddLineToPoint(context,yValue
  );
  CGContextAddLineToPoint(context,rect.size.height);
  CGContextFillPath(context);
}

我不知道这是否过度,所以也欢迎改进.此外,我得到了这个结果:

请注意,垂直线显示为绘制彼此相邻的子区域的结果.怎么可以避免?

解决方法

这个想法是正确的,但你不应该填充单个曲线,而是创建一个单独的路径然后填充它.所以,从CGContextMoveToPoint到左下角,CGContextAddLineToPoint到你的一个点,为所有曲线做CGContextAddCurveToPoint,最后,在右下角做一个CGContextAddLineToPoint(你也可以做一个CGContextClosePath for good测量).

这是一个简化的例子,我只有一个点数组,但它说明了这个想法:

- (void)drawRect:(CGRect)rect
{
    if (!self.points)
        [self configurePoints];

    DataPoint *point;

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGColorRef color = [[UIColor redColor] CGColor];
    CGContextSetstrokeColorWithColor(context,color);
    CGContextSetFillColorWithColor(context,color);

    point = self.points[0];

    CGContextMoveToPoint(context,point.x,self.bounds.size.height);
    CGContextAddLineToPoint(context,point.y);

    for (point in self.points)
    {
        // you'd do your CGContextAddCurveToPoint here; I'm just adding lines
        CGContextAddLineToPoint(context,point.y);
    }

    point = [self.points lastObject];
    CGContextAddLineToPoint(context,self.bounds.size.height);
    CGContextClosePath(context);

    CGContextDrawPath(context,kCGPathFillstroke);
}

大佬总结

以上是大佬教程为你收集整理的ios – 如何填充Quartz 2D中路径下方的区域?全部内容,希望文章能够帮你解决ios – 如何填充Quartz 2D中路径下方的区域?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。