【问题标题】:iOS - Clipping union of two paths using UIBezierPath appendPathiOS - 使用 UIBezierPath appendPath 剪切两条路径的联合
【发布时间】:2013-03-21 15:16:35
【问题描述】:

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

当我随后画一个圆时,这就是我希望最终绘图的样子:

      xXX| |XX
XXXX| |XXXX
XXXXX| |XXXXX
———      ———
———      ———
XXXXX| |XXXXX
XXXX| |XXXX
XX| |XXx

然而,它实际上是这样的:

      xXX| |XX
XXXX| |XXXX
XXXXX| |XXXXX
——— XX———
——— XX———
XXXXX| |XXXXX
XXXX| |XXXX
XX| |XXx

如果我正确阅读了此行为,则两条矩形路径的交点不会构成剪贴蒙版的一部分。

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

有人知道我能做什么吗?我已经包含了相关的代码sn-p。

使用一个路径绘制加号不是解决方案,因为我想将其他重叠路径添加到我的剪贴蒙版中。

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

【问题讨论】:

    标签: iphone ios graphics core-graphics uibezierpath


    【解决方案1】:

    您可以使用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);
    

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

    【讨论】:

    • 太棒了 - CGRectIntersection 是我所缺少的。谢谢
    猜你喜欢
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    • 2015-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-14
    相关资源
    最近更新 更多