首先,这是我目前情况的图片。
为了实现这一点,我使用具有以下方法的子类 UIView:
-(void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextBeginPath(ctx);
CGContextMoveToPoint (ctx, CGRectGetMinX(rect), CGRectGetMinY(rect)); // top left
CGContextAddLineToPoint(ctx, CGRectGetMaxX(rect), CGRectGetMidY(rect)); // mid right
CGContextAddLineToPoint(ctx, CGRectGetMinX(rect), CGRectGetMaxY(rect)); // bottom left
CGContextClosePath(ctx);
CGContextSetRGBFillColor(ctx, 0, 1, 0, 1);
CGContextFillPath(ctx);
}
我的最终目标是剪裁彩色区域,只留下霓虹绿。 (我稍后会翻转颜色)。我希望它看起来像饼图。我猜想有一种方法可以剪掉彩色的角,但我不知道。 CGContextClip(ctx) 不起作用。
我也在使用 Pixate 框架。如果有人知道用 Pixate 完成三角形形状的方法,那就更好了。
最佳答案
将 UIView 剪辑到路径的最简单方法是创建一个 CAShapeLayer,将路径添加到图层,并将形状图层添加为 View 图层的 mask 。
关于ios - 将自定义 UIView 裁剪为三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22262797/