Two coloured/custom line on MKPolylineRenderer

可紊 提交于 2019-12-21 20:38:00

问题


I am drawing x amount of lines on an MKMapView. The data is being downloaded from a webservice and for each line that needs drawing I am creating a MKPolyline, adding it to an overlayArray (some overlays have more than one polyline) and finally adding that overlay to the map via:

[self.mapView addOverlays:overlayArray];`

The next function is therefore called which works wonderfully:

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    if (![overlay isKindOfClass:[MKPolygon class]]) {
        MKPolyline *route = overlay;
        MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:route];
        renderer.strokeColor = [lineColours objectForKey:route.title];
        renderer.lineWidth = 3.0;
        return renderer;
    } else {
        return nil;
    }
}

As you can see, the strokeColor gets the correct UIColor from a pre-defined NSMutableDictionary. This all works as expected.

However, some of the lines overlap and using a single colour is not always desired. I was wondering if there was a way of creating a line made up of two or even 3 colours, but not a gradient along the line as seen in many fitness apps, but colours across the line. For example, another use would be to draw a motorway route made up of two white lines with a transparent strip in the middle.

This is quite important for an app I am developing so I will post any findings I find here, and I hope that other people can share their thoughts and knowledge too.

来源:https://stackoverflow.com/questions/21050358/two-coloured-custom-line-on-mkpolylinerenderer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!