2010-01-14 27 views
1

我正在使用iPhone的Quartz-2D在地图上显示路线。路线根据温度着色。由于某些街​​道的颜色是黄色,因此我在路线下方使用稍厚的黑色线条来创建边框效果,以便黄色街道上的黄色部分能够被点亮。但是,即使黑线与路线一样粗糙,整条路线看起来像是一条蠕虫(非常丑陋)。我想这是因为我正在绘制从航点到航点的线路,而不是使用最后一个航点作为下一个起点。这样,如果有几个路标遗失,路线仍然没有削减。iphone石英在彼此的顶部绘制2条线导致蠕虫效应

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

-(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. 

错误,我认为这是废话。

请提示我! :)


回答

0

听起来像这可能是与线帽有关。尝试绘制平坦的端盖,并看到它有帮助。

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

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]; 
    } 
}  
0

而且,我得到一个

<错误>:CGContextClosePath:没有当前点。

抚摸路径结束当前路径上下文。