多色线与核心图形

时间:2014-07-07 16:11:52

标签: ios core-graphics

我试图绘制一个有4个颜色段的条形图(不是渐变 - 4种不同的颜色)。这4个UIColor存储在一个数组中(我已经调试并检查了那些颜色是否正确设置)。出于某种原因,此循环仅绘制第一种颜色(以正确的宽度)。该类继承自UIView,并通过

调用
TopBar* bar = [[TopBar alloc]initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 20)];
[self.view addSubview:bar];

drawRect如下:

-(void)drawRect:(CGRect)rect{
NSLog(@"draw rect");
[super drawRect:rect];

CGContextRef ctx = UIGraphicsGetCurrentContext();

NSInteger fullBarWidth = self.frame.size.width;
NSInteger individualBarWidth = fullBarWidth/self.barColors.count;
NSInteger currentColorIndex = 0;

CGContextSetLineWidth(ctx, self.frame.size.height);
CGContextSetLineCap(ctx, kCGLineCapButt);


for (UIColor *color in self.barColors)
{
    NSInteger currentDrawLoc = currentColorIndex * individualBarWidth;
    currentColorIndex++;

    CGContextBeginPath(ctx);
    CGContextMoveToPoint(ctx, currentDrawLoc, 0);
    CGContextAddLineToPoint(ctx, individualBarWidth, 0);
    CGContextSetStrokeColorWithColor(ctx,color.CGColor);
    CGContextStrokePath(ctx);
}
}

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

在这一行:

CGContextAddLineToPoint(ctx, individualBarWidth, 0);

你想说:

CGContextAddLineToPoint(ctx, currentDrawLoc + individualBarWidth, 0);
相关问题