【发布时间】:2014-07-07 16:11:51
【问题描述】:
我正在尝试绘制一个具有 4 个颜色段(不是渐变 - 4 种不同颜色)的条形图。这 4 个 UIColors 存储在一个数组中(我已经调试并检查了这些颜色是否正确设置)。出于某种原因,此循环仅绘制第一种颜色(以正确的宽度)。这个类继承自 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);
}
}
任何帮助将不胜感激
【问题讨论】:
标签: ios core-graphics