iphone - 裁剪逆图?

标签 iphone geometry drawing core-graphics clipping

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

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

    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

最佳答案

一个简单的解决方案是使用 UIBazierPath。根据您的意图绘制一条巴齐尔路径。然后调用[path addClip]方法。它将剪掉闭合路径之外的所有内容。
例如,以下代码使您的 View 变成圆角。

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                           byRoundingCorners:UIRectCornerAllCorners
                                                 cornerRadii:CGSizeMake(6.0, 6.0)];
[path addClip];

关于iphone - 裁剪逆图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13553727/

相关文章:

c++ - 如何从位置和地标获取 (X,Y) vector

ios - 着色/绘图代码。如何以较低的不透明度使其更光滑?去除刷点?

c# - 在 C# 中更改 Jpeg 文件的大小

ios - 如何获得合适的、任意宽度的文本笔划?

iphone - 设备 (iPad) 上 OpenGL 纹理中的视觉伪影 - 但模拟器上没有

ios - 什么是 -applicationDidFinishLaunching 和 -applicationWillFinishLaunch?

javascript - 在鼠标位置放大 p5.js(包括 jsfiddle)

c# - 在 C# StreamGeometry Xaml 标记中创建非描边区域

iphone - 来自使用 GData 的搜索查询的 YouTube UITableView

iphone - navigationItem.backBarButtonItem 不工作?为什么以前的菜单仍然显示为按钮?