gpt4 book ai didi

iphone - iOS - 如何在 View 中绘制透明三角形

转载 作者:行者123 更新时间:2023-11-28 18:25:53 25 4
gpt4 key购买 nike

我正在尝试使用指向标签栏 View 的透明三角形创建自定义标签栏。

现在我正在 drawRect 方法中为此标签栏的背景绘制线性渐变和光泽。我只需要在那里添加透明三角形。我知道如何画三角形。我只需要知道如何使其透明以在标签栏 View 下方显示背景。

有人知道怎么做吗?

更新

这是当前代码:

void drawGlossAndGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor) 
{
drawLinearGradient(context, rect, startColor, endColor);

CGColorRef glossColor1 = [UIColor colorWithRed:1.0 green:1.0
blue:1.0 alpha:0.35].CGColor;
CGColorRef glossColor2 = [UIColor colorWithRed:1.0 green:1.0
blue:1.0 alpha:0.1].CGColor;

CGRect topHalf = CGRectMake(rect.origin.x, rect.origin.y,
rect.size.width, rect.size.height/2);

drawLinearGradient(context, topHalf, glossColor1, glossColor2);

}

void drawLinearGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor)
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat locations[] = { 0.0, 1.0 };

NSArray *colors = [NSArray arrayWithObjects:(__bridge id)startColor, (__bridge id)endColor, nil];

CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations);

CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));

CGContextSaveGState(context);
CGContextAddRect(context, rect);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
CGContextRestoreGState(context);

CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
}
- (void)drawTriangle
{
CGContextRef context = UIGraphicsGetCurrentContext();

CGPoint pt1 = CGPointMake(0.0f, 0.0f);
CGPoint pt2 = CGPointMake(10.0f, 10.0f);
CGPoint pt3 = CGPointMake(20.0f, 0.0f);

CGPoint vertices[] = {pt1, pt2, pt3, pt1};

CGContextBeginPath(context);
CGContextAddLines(context, vertices, 3);
CGContextClosePath(context);
CGContextClip(context);
}

- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();


CGColorRef lightColor = [UIColor colorWithRed:65.0f/255.0f green:64.0f/255.0f
blue:66.0f/255.0f alpha:1.0].CGColor;
CGColorRef darkColor = [UIColor colorWithRed:37.0/255.0 green:31.0/255.0
blue:32.0/255.0 alpha:1.0].CGColor;

[self drawTriangle];

CGRect viewRect = self.bounds;

drawGlossAndGradient(context, viewRect, lightColor, darkColor);
}

我添加了下面建议的剪辑,但这只会使我的渐变背景和光泽度消失,三角形变成灰色。有谁知道我在这里做错了什么?

最佳答案

如果你在 drawRect: 方法中绘制这个渐变,只需在它之前添加剪切路径。

示例:

-(void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGPoint vertices[] = {coordinates of vertices};

CGContextBeginPath(ctx);
CGContextAddLines(ctx, vertices, sizeof(vertices)/sizeof(CGPoint));
CGContextClosePath(ctx);
CGContextClip(ctx);

// draw the gradient
}

顶点 - 是一个有 7 个点的数组。 self.bounds 的每个角 1 个点和定义三角形的 3 个点。

例如:

   (0)  (1)  (3)           (4)
_____ ________________
| \ / |
| V |
| (2) |
(6) |_______________________|(5)

CGPoint vertices[7] = {CGPointZero, // origin
p1, p2, p3, // vertices of the triangle
{self.bounds.size.width, 0},
{self.bounds.size.width, self.bounds.size.height},
{0, self.bounds.size.height}
}

关于iphone - iOS - 如何在 View 中绘制透明三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9625125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com