MKPolyline / MKPolyline渲染器更改颜色动画

时间:2015-05-13 03:20:34

标签: ios mapkit mkpolyline

我正在处理与地图和折线相关的应用。 我想为折线的颜色变化设置动画,我要做的就是实现这一目标。

我昨天整天都在搜索正确的方法,并决定今天提问。

类似这样的代码可以在MKPolyline上执行:

//Assume that this view is a line shape.
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(50, 50, 100, 5);
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];

[UIView animateWithDuration:0.5f animations:^{
    view.backgroundColor = [UIColor greenColor];
}];

到目前为止,我有:

MKPolylineRenderer的子类

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
    CGFloat baseWidth = self.lineWidth / zoomScale;
    CGContextSetAlpha(context, 0.5);
    CGContextAddPath(context, self.path);
    CGContextSetStrokeColorWithColor(context, self.strokeColor.CGColor);
    CGContextSetLineWidth(context, baseWidth * 4);
    CGContextSetLineCap(context, self.lineCap);
    CGContextStrokePath(context);
    [super drawMapRect:mapRect zoomScale:zoomScale inContext:context];
}

在viewController.m中

添加折线

- (void)addPolylineToMap {
    CustomPolyline *polyline = [CustomPolyline polylineWithCoordinates:coordinates count:[coordinateArray count]];
    polyline.lineID = polylineID;
    [mapView addOverlay:polyline level:MKOverlayLevelAboveRoads];
}

更改折线颜色(感谢Anna #Ref:MKPolyline / MKPolylineRenderer changing color without remove it

- (void)changingPolylineColor {
    for (MKPolyline *overlays in [mapView overlays]) {
        if ([overlays isKindOfClass:[CustomPolyline class]]) {
            CustomPolylineRenderer *renderer = (CustomPolylineRenderer *)[mapView rendererForOverlay:overlays];
            renderer.strokeColor = [self getColorFromArrayWithLineID:[(CustomPolyline *)overlays lineID]];
            [renderer invalidatePath];
        }
    }
}

MapViewDelegate renderForOverlay:

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
    if ([overlay isKindOfClass:[CustomPolyline class]]) {
        polylineView = [[CustomPolylineRenderer alloc] initWithPolyline:overlay];
        polylineView.strokeColor = [self getColorFromArrayWithLineID:[(CustomPolyline *)overlay lineID]];
        polylineView.alpha = 0.5f;
        polylineView.lineWidth = 4.0f;
        return polylineView;
    }
    return nil;
}

所以我需要的是当我打电话给[self changingPolylineColor];时,地图上的折线会随着动画改变颜色。

提前谢谢。

0 个答案:

没有答案