剪切反向绘图?

3

我想要删除以下图像中的蓝色部分。如何剪辑绘图的反向部分?(我相信这是正确的提问方式)

三角形的示例代码:(如果有更好的三角形代码,我也会接受!;)

    int lineWidth = 4;

    int w = size.size.width;
    int h = size.size.height - lineWidth;

    CGPoint top = CGPointMake(0+(w/2)+.5, 0);

    CGContextClipToRect(ctx, CGRectMake(0, 0, w, h));
    CGContextStrokePath(ctx);

    CGContextMoveToPoint(ctx, top.x, top.y);
    CGContextAddLineToPoint(ctx, top.x + (w/2), top.y + h  );
    CGContextAddLineToPoint(ctx, top.x - (w/2), top.y + h   );
    CGContextAddLineToPoint(ctx, top.x, top.y);

    CGContextFillPath(ctx);

    CGContextSetLineWidth(ctx, lineWidth);
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGContextMoveToPoint(ctx, top.x, top.y);
    CGContextAddLineToPoint(ctx, top.x + (w/2), top.y + h  );
    CGContextAddLineToPoint(ctx, top.x - (w/2), top.y + h   );
    CGContextAddLineToPoint(ctx, top.x, top.y);

    CGContextStrokePath(ctx);

enter image description here

2个回答

1
一个简单的解决方案是使用UIBazierPath。根据您的意图绘制一条Bazier路径。然后调用[path addClip]方法。它将剪切所有在封闭路径之外的内容。
例如,以下代码可以使您的视图变成圆角。
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                           byRoundingCorners:UIRectCornerAllCorners
                                                 cornerRadii:CGSizeMake(6.0, 6.0)];
[path addClip];

0

要剪辑任何路径的反向,只需将一个封闭矩形添加到您的剪辑中。

在这种情况下,如果我理解正确,您想要围绕形状的红色描边进行剪辑。通常,在石英中由于描边路径的线宽,这不是一项容易的任务。当然,对于这两个形状,我们可以计算出红色描边的轮廓并围绕其进行剪辑。但是,一般的解决方案需要一种计算包含路径(包括线宽)的方法。

还有其他方法,包括位图掩码,但最好的解决方案取决于您正在绘制的内容。我曾经遇到过类似的问题,可以使用专门的石英混合模式非常优雅和高效地解决。这在绘制之前的上下文内容以及结果应如何与UI的其余部分混合非常依赖于。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接