【问题标题】:Multicolor line with Core Graphics带有核心图形的多色线
【发布时间】: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


    【解决方案1】:

    在这一行:

    CGContextAddLineToPoint(ctx, individualBarWidth, 0);
    

    你的意思是:

    CGContextAddLineToPoint(ctx, currentDrawLoc + individualBarWidth, 0);
    

    【讨论】:

    • 完美!非常感谢。我会在 5 分钟内接受这个
    猜你喜欢
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多