代码之家  ›  专栏  ›  技术社区  ›  Lee Armstrong

核心图形多色线条

  •  4
  • Lee Armstrong  · 技术社区  · 15 年前

    我有以下代码,似乎只有最后一种颜色用于整行…… 我想让颜色在这期间一直在变化。有什么想法吗?

            CGContextSetLineWidth(ctx, 1.0);
    
            for(int idx = 0; idx < routeGrabInstance.points.count; idx++)
            {
                CLLocation* location = [routeGrabInstance.points objectAtIndex:idx];
    
                CGPoint point = [mapView convertCoordinate:location.coordinate toPointToView:self.mapView];
    
                if(idx == 0)
                {
                    // move to the first point
                    UIColor *tempColor = [self colorForHex:[[routeGrabInstance.pointHeights objectAtIndex:idx] doubleValue]];
                    CGContextSetStrokeColorWithColor(ctx,tempColor.CGColor);
                    CGContextMoveToPoint(ctx, point.x, point.y);
    
                }
                else
                {
                        UIColor *tempColor = [self colorForHex:[[routeGrabInstance.pointHeights objectAtIndex:idx] doubleValue]];
                        CGContextSetStrokeColorWithColor(ctx,tempColor.CGColor);
                        CGContextAddLineToPoint(ctx, point.x, point.y);
                }
            }
    
            CGContextStrokePath(ctx);
    
    2 回复  |  直到 15 年前
        1
  •  4
  •   loomer    15 年前

    cgContextSetStrokeColorWithColor只更改上下文的状态,不进行任何绘图。代码中唯一完成的绘图是由末尾的CGContextStrokePath完成的。由于每次调用CGContextSetStrokeColorWithColor都会覆盖上一个调用所设置的值,因此图形将使用最后一个颜色集。

    您需要创建一个新的路径,设置颜色,然后在每个循环中绘制。像这样:

    for(int idx = 0; idx < routeGrabInstance.points.count; idx++)
    {
        CGContextBeginPath(ctx);
        CGContextMoveToPoint(ctx, x1, y1);
        CGContextAddLineToPoint(ctx, x2, y2);
        CGContextSetStrokeColorWithColor(ctx,tempColor.CGColor);
        CGContextStrokePath(ctx);
    }
    
        2
  •  -1
  •   Michal    15 年前

    CGContextSetStrokeColorWithColor 设置上下文中的笔划颜色。该颜色在绘制路径时使用,在继续构建路径时不起作用。

    你需要把每一行分开划( CGContextStrokePath )