【问题标题】:iOS - How to draw a transparent triangle in viewiOS - 如何在视图中绘制透明三角形
【发布时间】:2012-03-08 21:22:43
【问题描述】:

我正在尝试使用指向标签栏视图的透明三角形制作自定义标签栏。

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

有人知道怎么做吗?

更新

这是当前代码:

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

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

【问题讨论】:

  • 一些代码会对你有所帮助> I know how to draw a triangle. -> 所以贴出来

标签: iphone objective-c ios core-graphics


【解决方案1】:

如果你在 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
}

Vertices - 是一个包含 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}
}

【讨论】:

  • 我按照你的建议添加了剪辑,但它不会绘制我的背景并使三角形变灰??
  • 对不起,我的错。当然,顶点不应该代表三角形,而是代表反向区域。看看我更新的答案。
【解决方案2】:

如果有人需要,这是一个无需使用 UIImageView 在 UITableView 中绘制彩色三角形的代码。这种方法很好,因为三角形的大小可以变化并且以编程方式绘制。此外,您还可以更改颜色并使三角形透明。

@implementation RRTriangleView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

      CGMutablePathRef path = CGPathCreateMutable();
      CGPathMoveToPoint(path, NULL, 0.0, 0.0);
      CGPathAddLineToPoint(path, NULL, - self.bounds.size.height / 2.0, self.bounds.size.height / 2.0);
      CGPathAddLineToPoint(path, NULL, 0.0, self.bounds.size.height);
      CGPathAddLineToPoint(path, NULL, 0.0f, 0.0f);

      CAShapeLayer *shapeLayer = [CAShapeLayer layer];
      [shapeLayer setPath:path];
      [shapeLayer setFillColor:[COLOR_CUSTOM_LIGHT_BLUE CGColor]];
      [shapeLayer setStrokeColor:[COLOR_CUSTOM_LIGHT_BLUE CGColor]];
      [shapeLayer setPosition:CGPointMake(self.bounds.size.width, 0.0f)];
      [[self layer] addSublayer:shapeLayer];

      CGPathRelease(path);

    }
    return self;
}

初始化这个 TriangleView 并将其添加到您的单元格中,当它被选中时:

- (RRTriangleView *)triangleView
{
  if (! _triangleView) {

    _triangleView = [[RRTriangleView alloc] initWithFrame:self.bounds];
    _triangleView.layer.backgroundColor = [[UIColor clearColor] CGColor];
    _triangleView.clipsToBounds = NO;

  }

  return _triangleView;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
  //[super setSelected:selected animated:animated];

  if (selected) {

    [self addSubview:self.triangleView];

  }
  else {

    [self.triangleView removeFromSuperview];

  }
}

triangleView 的大小就像你的单元格的视图,它是透明的,并且是在上面绘制的。

【讨论】:

    【解决方案3】:

    我相信

    [view setBackgroundColor:[UIColor clearColor]];
    [view setOpaque:NO];
    

    将使您的视图透明。

    【讨论】:

    • 整个视图不需要是透明的...只是在视图上绘制的三角形。
    • 我不明白。你想画一个完全透明的三角形吗?
    • 是的,在具有黑色背景的视图上。视图本身是不透明的。它是黑色的。但在这个视图的背后是一个图像。我希望能够通过那个三角形看到那个图像。所以只是三角形是完全透明的。
    • 啊,现在我明白了。您必须创建一个CALayer 掩码才能执行此操作,或者也可以使用图像。
    • 好的,那么你如何使用 CALayer 掩码来做到这一点?
    猜你喜欢
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 2015-01-26
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多