iOS - 使用UIBezierPath appendPath剪切两个路径的并集

D A*_*D A 6 iphone graphics core-graphics ios uibezierpath

我正在尝试以加号的形式创建剪切路径,以便我在相同上下文中绘制的后续路径删除了此部分.我使用两个相互重叠的矩形路径创建剪切路径.

这是我在随后绘制圆圈时最终绘制的图形:

      XXX | | XXx
   XXXX | | XXXX
 XXXXX | | XXXXX
 --- ---
 --- ---
 XXXXX | | XXXXX
   XXXX | | XXXX
      xXX | | XXX

但是,它实际上看起来像这样:

      XXX | | XXx
   XXXX | | XXXX
 XXXXX | | XXXXX
 --- XX ---
 --- XX ---
 XXXXX | | XXXXX
   XXXX | | XXXX
      xXX | | XXX

如果我正确读取此行为,两个矩形路径的交集不会形成剪切蒙版的一部分.

似乎(毫不奇怪)appendPath在这种情况下不会从我的两个矩形路径创建一个统一的路径 - 我假设我无能为力.此外,Core Graphics似乎没有任何与路径联盟等相关的功能.

有谁知道我能做什么?我已经包含了相关的代码段.

使用一条路径绘制加号不是解决方案,因为我想在剪贴蒙版中添加其他重叠路径.

        CGContextSaveGState(context);

        // create clipping path
        UIBezierPath *clippingPath = [UIBezierPath bezierPath];
        clippingPath = [UIBezierPath bezierPathWithRect:CGRectMake(centrePoint.x - 2.0f, 0.0f, 4.0f, self.sizeY)];
        [clippingPath appendPath:[UIBezierPath bezierPathWithRect:CGRectMake(0.0f, centrePoint.y - 2.0f, self.sizeX, 4.0f)]];

        // use the clipping path to create a hole in the context
        CGContextAddPath(context, clippingPath.CGPath);
        CGRect boundingRect = CGContextGetClipBoundingBox(context);
        CGContextAddRect(context, boundingRect);
        CGContextEOClip(context);

        // draw the icon shape (clipped portion is removed)
        iconBezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(self.sizeX / 3.0f, self.sizeY / 2.25f, self.sizeX / 3.0f, self.sizeX / 3.0f)];

        [highlightColor setFill];
        [iconBezierPath fill];
        CGContextRestoreGState(context);
Run Code Online (Sandbox Code Playgroud)

fou*_*dry 3

您可以使用以下命令添加回被交叉点淘汰的部分CGRectIntersection

    CGContextSaveGState(context);

    CGRect rect1 = CGRectMake(centrePoint.x - 2.0f, 0.0f, 4.0f, self.sizeY);
    CGRect rect2 = CGRectMake(0.0f, centrePoint.y - 2.0f, self.sizeX, 4.0f);
    CGRect rect3 = CGRectIntersection(rect1, rect2);

    CGContextAddRect(context, rect1);
    CGContextAddRect(context, rect2);
    CGContextAddRect(context, rect3);

    CGRect boundingRect = CGContextGetClipBoundingBox(context);
    CGContextAddRect(context, boundingRect);
    CGContextEOClip(context);

        // draw the icon shape (clipped portion is removed)
    iconBezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(self.sizeX / 3.0f, self.sizeY / 2.25f, self.sizeX / 3.0f, self.sizeX / 3.0f)];

    [highlightColor setFill];
    [iconBezierPath fill];

    CGContextRestoreGState(context);
Run Code Online (Sandbox Code Playgroud)

这满足了你的问题的要求,但它是否完全满足你的需求取决于“其他重叠路径”的性质。

在此输入图像描述