2012-03-08 47 views
0

我正在尝试使用指向标签栏视图的透明三角形执行自定义选项卡栏。iOS - 如何在视图中绘制透明三角形

现在我正在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); 
} 

我加剪辑以下建议,但只是做了我的背景梯度和光泽dissappear和三角形变成灰色。有人知道我在这里做错了吗?

+3

一些代码会帮助你>'我知道如何绘制一个三角形.' - >所以发布它 – dom 2012-03-08 21:37:10

+0

上面添加的代码在更新中。 – theSpectre142 2012-03-09 02:03:51

回答

7

如果您在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} 
} 
+0

我按照你的建议添加了剪辑,但它不会绘制我的背景并使三角形变成灰色? – theSpectre142 2012-03-09 02:04:20

+0

对不起,我的错。因为顶点应该不是三角形而是反面区域。看看我更新的答案。 – 2012-03-09 12:03:22

+0

甜!这工作!谢谢! – theSpectre142 2012-03-09 21:32:56

-1

我beleive是

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

会让你的看法是透明的。

+0

整个视图不需要透明...只是在视图上绘制的三角形。 – theSpectre142 2012-03-08 21:59:46

+0

我不明白。你想绘制一个完全透明的三角形? – akashivskyy 2012-03-08 22:03:53

+0

是的,在黑色背景的视图上。视图本身不透明。它是黑色的。但在这个观点背后是一个形象。我希望能够通过那个三角形看到那个图像。所以只是三角形是完全透明的。 – theSpectre142 2012-03-08 22:08:59

0

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

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

Init这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的大小就像是你的认为,它是透明的,它上面绘制。