2013-12-09 109 views

回答

8

一个简单的方法可以是具有不同的UIImageView的含有有色三角形,然后选择不同的图像,根据某些值/偏好您设置显示量。 这里采取:Drawing a triangle in UIView

你将需要做一些数学来计算正确的点,但这是绘制三角形的一般方法。你可以创建自己的UIView的子类(称之为CornerTriangle),然后将其添加到单元格,同时设置其颜色以符合您的需求。

-(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, 1, 1, 0, 1); 
    CGContextFillPath(ctx); 
} 
+0

你好:)我想绘制三角形使用CGRect而不是添加UIImageViews。我刚刚编辑了我的问题。 :) –

+0

当然,这个自定义视图应该有一个颜色属性,并且该属性将在'drawRect:'方法中用于绘制三角形。 – rmaddy

+0

@KeithOYS嗨基思,如果我的解决方案帮助你请upvote /批准。感谢芽! – royherma

0

添加下面的方法到你的UITableViewCell类。这是你正在寻找的正确效果:

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

    CGContextBeginPath(ctx); 
    CGContextMoveToPoint (ctx, CGRectGetMaxX(rect), CGRectGetMidY(rect)); 
    CGContextAddLineToPoint(ctx, CGRectGetMaxX(rect), CGRectGetMinY(rect)); 
    CGContextAddLineToPoint(ctx, CGRectGetMaxX(rect)-25, CGRectGetMinY(rect)); 
    CGContextClosePath(ctx); 

    CGContextSetRGBFillColor(ctx, 1, 1, 0, 1); 
    CGContextFillPath(ctx); 
}