MKPolyline/MKPolylineRenderer更改颜色而不将其删除 [英] MKPolyline / MKPolylineRenderer changing color without remove it

查看:272
本文介绍了MKPolyline/MKPolylineRenderer更改颜色而不将其删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理地图应用程序,我想问一下如何更改折线颜色而不将其删除并再次添加它,我发现了这个主题 http://stackoverflow.com/questions/24226290/mkpolylinerenderer-change-color-without-removing-overlay 但这不涉及我的问题,我没有碰到这条线,因此无需使用-[MKMapViewDelegate mapView:didSelectAnnotationView:]

I working around with map app, I want to ask how to change the polyline color without remove and add it again, I found this topic https://stackoverflow.com/questions/24226290/mkpolylinerenderer-change-color-without-removing-overlay in stackoverflow but this is not involve with my question, I did not touch the line, so no need to do with -[MKMapViewDelegate mapView:didSelectAnnotationView:]

那么有可能这样做吗?

我想做的是平滑地改变多段线的颜色(通过阴影着色-听起来像动画)如果您对如何设置该多段线的动画有任何想法,也请告诉我.谢谢

What I want to do is change the polyline color smoothly (by shading a color - sound like an animation) If you have any idea on how to animate this polyline please also tell me too. Thanks

推荐答案

复杂的动画或阴影/渐变可能需要创建自定义的叠加层渲染器类.

Complex animations or shading/gradients will probably require creating a custom overlay renderer class.

这些其他答案也提供了有关如何绘制渐变折线的想法,并且动画也非常需要自定义叠加层渲染器:

These other answers give ideas about how to draw gradient polylines and animations will most like require a custom overlay renderer as well:

  • how to customize MKPolyLineView to draw different style lines
  • Gradient Polyline with MapKit ios
  • Draw CAGradient within MKPolyLineView

Apple的Breadcrumb示例应用程序有一个自定义渲染器的示例,您可能会发现它有用.

Apple's Breadcrumb sample app also has an example of a custom renderer which you may find useful.


但是,如果您只想更新线条的颜色(例如,从蓝色更改为红色),则可以执行以下操作:


However, if you just want to update the line's color (say from blue to red), then you may be able to do that as follows:

  1. 获取要更改的MKPolyline的引用.
  2. 获取在步骤1中获得的折线的MKPolylineRenderer引用.这可以通过调用地图视图的rendererForOverlay:实例方法(与mapView:rendererForOverlay:委托方法不同)来实现.
  3. 更新渲染器的strokeColor.
  4. 在渲染器上调用invalidatePath.
  1. Get a reference to the MKPolyline you want to change.
  2. Get a reference to the MKPolylineRenderer for the polyline obtained in step 1. This can be done by calling the map view's rendererForOverlay: instance method (not the same as the mapView:rendererForOverlay: delegate method.
  3. Update the renderer's strokeColor.
  4. Call invalidatePath on the renderer.

不确定您想要什么,但是您可以通过更改颜色并按时间顺序逐步调用invalidatePath来动画化"从蓝色变为红色的颜色.

Not sure what you want but you may be able to "animate" the color going from blue to red by changing the color and calling invalidatePath gradually in timed steps.

另一件事是要确保rendererForOverlay delegate 方法也使用该行的当前"颜色,以防在您更改了渲染器的strokeColor后地图视图调用委托方法的情况下直接地.

Another important thing is to make sure the rendererForOverlay delegate method also uses the line's "current" color in case the map view calls the delegate method after you've changed the renderer's strokeColor directly.

否则,在平移或缩放地图后,折线的颜色将变回委托方法中设置的颜色.

Otherwise, after panning or zooming the map, the polyline's color will change back to whatever's set in the delegate method.

您可以将线条的当前颜色保留在类级别的变量中,并在委托方法和您要更改线条颜色的位置中使用它.

You could keep the line's current color in a class-level variable and use that in both the delegate method and the place where you want to change the line's color.

类级变量的替代方法(可能更好)是使用MKPolyline的title属性保留其颜色,或者使用具有color属性的自定义折线叠加层类(而非渲染器).

An alternative to a class-level variable (and probably better) is to either use the MKPolyline's title property to hold its color or a custom polyline overlay class (not renderer) with a color property.

示例:

@property (nonatomic, strong) UIColor *lineColor;
//If you need to keep track of multiple overlays, 
//try using a NSMutableDictionary where the keys are the 
//overlay titles and the value is the UIColor.

-(void)methodWhereYouOriginallyCreateAndAddTheOverlay
{
    self.lineColor = [UIColor blueColor];  //line starts as blue
    MKPolyline *pl = [MKPolyline polylineWithCoordinates:coordinates count:count];
    pl.title = @"test";
    [mapView addOverlay:pl];
}

-(void)methodWhereYouWantToChangeLineColor
{
    self.lineColor = theNewColor;

    //Get reference to MKPolyline (example assumes you have ONE overlay)...
    MKPolyline *pl = [mapView.overlays objectAtIndex:0];

    //Get reference to polyline's renderer...
    MKPolylineRenderer *pr = (MKPolylineRenderer *)[mapView rendererForOverlay:pl];
    pr.strokeColor = self.lineColor;
    [pr invalidatePath];
}

-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKPolyline class]]) {
        MKPolylineRenderer *pr = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
        pr.strokeColor = self.lineColor;
        pr.lineWidth = 5;
        return pr;
    }

    return nil;
}

这篇关于MKPolyline/MKPolylineRenderer更改颜色而不将其删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆