2

在将 MKOverlayRenderer 的 alpha 属性传递给地图后,如何访问和修改它?

事实上,我可以修改rendererForOverlay方法中的 alpha 属性:

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id <MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKTileOverlay class]]) {

        MKTileOverlayRenderer *renderer = [[MKTileOverlayRenderer alloc] initWithTileOverlay:overlay];
        renderer.alpha = 0.6;
        return renderer;
    }
    return nil;
}

但是只有当我向 mapView 添加 Overlay 时才会调用此方法,对吧?

所以我的问题是:即使在我的 Overlay 已经在地图上渲染之后,有没有办法改变和设置这个 alpha 属性的值?

我尝试将渲染器添加到 NSMutableArray 中:

 - (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id <MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKTileOverlay class]]) {
        _allRenderer = [[NSMutableArray alloc] init];
        MKTileOverlayRenderer *renderer = [[MKTileOverlayRenderer alloc] initWithTileOverlay:overlay];

        renderer.alpha = 0.0;
        [_allRenderer addObject:renderer];
        return renderer;
    }
    return nil;
}

然后我可以通过调用我所做的这个方法来改变透明度:

-(void)changeAlpha:(NSUInteger)index : (BOOL)isOpaque {

    if (isOpaque)
        [[_allRenderer objectAtIndex:index] setAlpha:0.0];
    else
        [[_allRenderer objectAtIndex:index] setAlpha:1.0];
}

有没有更好的方法来做到这一点?仅设置 alpha 值似乎是一个缓慢的过程。

实际上我想动态地将我的 OverlayRenderer 的 alpha 值从 0 切换到 1,这样我就可以显示 1 Overlay,隐藏它,显示另一个,隐藏它等等......

4

2 回答 2

1

所以基本上你可以做的是:而不是试图设置不同的alpha。

您可以在覆盖和删除它以及为 alpha 设置初始值之间切换。

作为参考,请尝试此链接: https ://github.com/mapbox/mbxmapkit/issues/7

还有这个:https ://github.com/mapbox/mbxmapkit/issues/39

于 2016-10-14T09:37:17.617 回答
0

Ok so I worked with adding/removing the MKTileOverlay instead of playing with the alpha value, as @Sharpkits mentioned in the comments.

I almost got the expected result. I'm just facing an issue with the first time the tiles are rendered, I got some glitches, because the rendering is tile by tile.

I think I should display a spinner for the first 2 seconds while the Overlays are rendering.

After that delay, my animation with the tile images is playing smoothly.

I still got the glitch effect when I zoom in/out, because new tiles have to be rendered.

Some context

Basically I would like to make an animation with 10 images. All the images are cut according to specific zoom level, specific positions and with 15 minutes time interval.

I have a UISlider and a Play/Pause Button, and I'm removing or adding specific Overlay, according to the position of my Slider while the animation is playing. With tapGestureRecognizer on my Slider, I can go back and further in the "video" while I'm displaying the Overlay that has to be displayed.

-(void)showOverlay:(NSUInteger)index {

    for (int i = 0; i < [_allTileOverlay count]; i++) {
        if (i != index)
            [_mapView removeOverlay:[_allTileOverlay objectAtIndex:i]];
        else
            [_mapView addOverlay:[_allTileOverlay objectAtIndex:index]];
    }
}

And this is the part where I'm displaying the Overlay according to the position of my UISlider while the animation is playing.

if ([_allTileOverlay count]>0) {
        if (_currentSlider.value>=[_allTileOverlay count]-1) {
            dispatch_async(dispatch_get_main_queue(), ^(void){
                //Run UI Updates
                [self showOverlay:0];
            });
            _currentSlider.value=0;
        }else{
            //scroll to desire position
            _currentSlider.value++;
            dispatch_async(dispatch_get_main_queue(), ^(void){
                //Run UI Updates
                [self showOverlay:_currentSlider.value];
            });
        }
    }

There is some work to do in order to finish the feature, but I will close this question because I got all the informations I expected.

于 2016-10-14T13:55:53.050 回答