MKOverlay not resizing smoothly

你。 提交于 2020-01-19 23:22:44

问题


I have added a MKCircle as MKOverlay to my MKMapView. Also I added an UISlider to decide the radius of the circle. Unfortunately when using this it seems a bit "laggy", not smootly like I want it to be.

Example: http://dl.dropbox.com/u/3077127/mkoverlayDelay.mov

This is my code:

- (void)addCircle
{
    // draw the radius circle for the marker
    double radius = 2000.0;
    MKCircle *circle = [MKCircle circleWithCenterCoordinate:location radius:radius];
    [circle setTitle:@"background"];
    [mapView addOverlay:circle];

    MKCircle *circleLine = [MKCircle circleWithCenterCoordinate:location radius:radius];
    [circleLine setTitle:@"line"];
    [mapView addOverlay:circleLine];
}

- (void)addCircleWithRadius:(double)radius
{
    MKCircle *circle = [MKCircle circleWithCenterCoordinate:location radius:radius];
    [circle setTitle:@"background"];
    [mapView addOverlay:circle];

    MKCircle *circleLine = [MKCircle circleWithCenterCoordinate:location radius:radius];
    [circleLine setTitle:@"line"];
    [mapView addOverlay:circleLine];
}

- (void)sliderChanged:(UISlider*)sender
{
    [mapView removeOverlays:[mapView overlays]];

    double radius = (sender.value * 100);

    [self addCircleWithRadius:radius];
}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay{
    MKCircle *circle = overlay;
    MKCircleView *circleView = [[[MKCircleView alloc] initWithCircle:overlay] autorelease];

    if ([circle.title isEqualToString:@"background"])
    {
        circleView.fillColor = UIColorFromRGB(0x598DD3);
        circleView.alpha = 0.25;
    }
    else
    {
        circleView.strokeColor = UIColorFromRGB(0x5C8AC7);
        circleView.lineWidth = 2.0;
    }

    return circleView;
}

Does anybody have any suggestions on how I can smoothen this?

Best regards,
Paul Peelen


回答1:


I have tried your code and found a very easy way to make it smoother.

If you change the order of the calls in: - (void)sliderChanged:(UISlider*)sender

You can call [self addCircleWithRadius:radius];

before calling [mapView removeOverlays:[mapView overlays]];

Just make sure you dont remove the overlays you just added, only the old ones.

This will give you a smoother resizing, specially when the new circle is smaller than the old one.

For circles that are bigger you are probably better off using NSOperations to ensure the views are created faster, this will make it smoother.

Hope this helps.




回答2:


Your best bet might be to draw the circle yourself, either using another UIView on top of the MKMapView, or using a MKAnnotationView within the Map View.

This blogger has done a similar thing (before iOS4 when overlays were added). http://spitzkoff.com/craig/?p=65



来源:https://stackoverflow.com/questions/4876035/mkoverlay-not-resizing-smoothly

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