iphone石英绘制2条线在彼此之上导致蠕虫效应

时间:2010-01-14 00:41:45

标签: iphone drawing line quartz-2d routes

我正在使用Quartz-2D for iPhone在地图上显示路线。路线根据温度着色。因为有些街道是黄色的,所以我在路线下使用稍粗的黑线来创建边框效果,这样路线的黄色部分就可以在黄色街道上找到。但是,即使黑线与路线一样粗,整条路线看起来像一只蠕虫(非常难看)。我认为这是因为我正在从航点到航点画线,而是使用最后一个航点作为下一个起点航点。这样,如果缺少几个航路点,路线仍然没有削减。

如果没有蠕虫效应,我需要做什么来显示两条线?

-(void) drawRect:(CGRect) rect
{
CSRouteAnnotation* routeAnnotation = (CSRouteAnnotation*)self.routeView.annotation;

// only draw our lines if we're not int he moddie of a transition and we 
// acutally have some points to draw. 
if(!self.hidden && nil != routeAnnotation.points && routeAnnotation.points.count >   0)
{
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    Waypoint* fromWaypoint = [[Waypoint alloc] initWithDictionary:[routeAnnotation.points objectAtIndex:0]];
    Waypoint* toWaypoint;

    for(int idx = 1; idx < routeAnnotation.points.count; idx++)
    {
        toWaypoint = [[Waypoint alloc] initWithDictionary:[routeAnnotation.points objectAtIndex:idx]];


        CLLocation* fromLocation = [fromWaypoint getLocation];
        CGPoint fromPoint = [self.routeView.mapView convertCoordinate:fromLocation.coordinate toPointToView:self];

        CLLocation* toLocation = [toWaypoint getLocation];
        CGPoint toPoint = [self.routeView.mapView convertCoordinate:toLocation.coordinate toPointToView:self];

        routeAnnotation.lineColor = [fromWaypoint.weather getTemperatureColor];

        CGContextBeginPath(context);
        CGContextSetLineWidth(context, 3.0);
        CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
        CGContextMoveToPoint(context, fromPoint.x, fromPoint.y);
        CGContextAddLineToPoint(context, toPoint.x, toPoint.y);
        CGContextStrokePath(context);
        CGContextClosePath(context);

        CGContextBeginPath(context);
        CGContextSetLineWidth(context, 3.0);
        CGContextSetStrokeColorWithColor(context,    routeAnnotation.lineColor.CGColor);
        CGContextMoveToPoint(context, fromPoint.x, fromPoint.y);
        CGContextAddLineToPoint(context, toPoint.x, toPoint.y);
        CGContextStrokePath(context);
        CGContextClosePath(context);


        fromWaypoint = toWaypoint;
    }

    [fromWaypoint release];
    [toWaypoint release];       
}
}

另外,我得到了

<Error>: CGContextClosePath: no current point.

错误,我认为是胡说八道。

请提示我! :)


4 个答案:

答案 0 :(得分:0)

听起来这可能与线帽有关。尝试使用扁平端盖绘图,看看它有帮助。

更新: 我看到现在发生了什么。它只是“背景”线与前一个“背景”+“前景”线条绘制所覆盖的像素重叠。您更改绘制所有“背景”行然后所有“前景”行避免此问题。 你的线条仍然覆盖了一些像素,但只有相同的颜色,所以你不会注意到。 (请注意,大多数线端帽样式都延伸到线条中的实际点,使效果更加明显 - 像素重叠更多。)

答案 1 :(得分:0)

您可以只绘制一条并使用the Shadows ability吗?

,而不是绘制两条线

答案 2 :(得分:0)

谢谢你们!因此,简单地添加阴影或线帽并没有帮助,效果保持不变。但是,我只是稍微使用了代码,事实证明,如果我分别在完整路径中绘制两条线(在两个for循环中),效果就消失了!然后我在背景线上添加了方形线帽,使其更加漂亮。

现在我唯一想知道的是......对此有何解释?这是新代码,我稍后会优化它......

-(void) drawRect:(CGRect) rect {
CSRouteAnnotation* routeAnnotation = (CSRouteAnnotation*)self.routeView.annotation;

// only draw our lines if we're not int he moddie of a transition and we 
// acutally have some points to draw. 
if(!self.hidden && nil != routeAnnotation.points && routeAnnotation.points.count > 0)
{
    CGContextRef context = UIGraphicsGetCurrentContext(); 


    Waypoint* fromWaypoint = [[Waypoint alloc] initWithDictionary:[routeAnnotation.points objectAtIndex:0]];
    Waypoint* toWaypoint;

    for(int idx = 1; idx < routeAnnotation.points.count; idx++)
    {
        toWaypoint = [[Waypoint alloc] initWithDictionary:[routeAnnotation.points objectAtIndex:idx]];


        CLLocation* fromLocation = [fromWaypoint getLocation];
        CGPoint fromPoint = [self.routeView.mapView convertCoordinate:fromLocation.coordinate toPointToView:self];

        CLLocation* toLocation = [toWaypoint getLocation];
        CGPoint toPoint = [self.routeView.mapView convertCoordinate:toLocation.coordinate toPointToView:self];

        routeAnnotation.lineColor = [fromWaypoint.weather getTemperatureColor];

        CGContextBeginPath(context);
        CGContextSetLineWidth(context, 3.5);
        CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
        CGContextSetLineCap(context, kCGLineCapSquare);
        CGContextMoveToPoint(context, fromPoint.x, fromPoint.y);
        CGContextAddLineToPoint(context, toPoint.x, toPoint.y);
        CGContextStrokePath(context);
        CGContextClosePath(context);

        fromWaypoint = toWaypoint;

    }

    // zweite for schleife
    for(int idx = 1; idx < routeAnnotation.points.count; idx++)
    {
        toWaypoint = [[Waypoint alloc] initWithDictionary:[routeAnnotation.points objectAtIndex:idx]];


        CLLocation* fromLocation = [fromWaypoint getLocation];
        CGPoint fromPoint = [self.routeView.mapView convertCoordinate:fromLocation.coordinate toPointToView:self];

        CLLocation* toLocation = [toWaypoint getLocation];
        CGPoint toPoint = [self.routeView.mapView convertCoordinate:toLocation.coordinate toPointToView:self];

        routeAnnotation.lineColor = [fromWaypoint.weather getTemperatureColor];

        CGContextBeginPath(context);

        CGContextSetLineWidth(context, 3.0);
        CGContextSetStrokeColorWithColor(context, routeAnnotation.lineColor.CGColor);
        CGContextSetLineCap(context, kCGLineCapSquare );
        CGContextMoveToPoint(context, fromPoint.x, fromPoint.y);
        CGContextAddLineToPoint(context, toPoint.x, toPoint.y);
        CGContextStrokePath(context);
        CGContextClosePath(context);

    }

    [fromWaypoint release];
   }
}       

答案 3 :(得分:0)

  

另外,我得到了

     

&lt;错误&gt;:CGContextClosePath:没有当前点。

描边路径结束当前路径上下文。