Search code examples
iosobjective-cmkmapviewmkoverlay

Resizing MKCircle on MKMapView is flickering


I'm trying to solve a problem since few days and I've got no good results. I've got a point and a circle on MKMapView. I've got UISlider and want to change size of MKCircle. Size is changed but during resizing this circle flickers and blinks.

Here is my code:

@implementation ViewController {
    Annotation *_annotation;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _annotation = [[Annotation alloc] init];
    [_annotation setCoordinate: CLLocationCoordinate2DMake(0, 0)];
    [self.mapView addAnnotation:_annotation];
    [self.mapView setCenterCoordinate:_annotation.coordinate animated:YES];

    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(_annotation.coordinate, 1000, 1000);
    [self.mapView setRegion:region];

    [self _addCircleOnCurrentLocationWithRadius:_slider.value];
}

- (IBAction)onSliderChanged:(UISlider *)sender {
    [self.mapView removeOverlays:self.mapView.overlays];
    [self _addCircleOnCurrentLocationWithRadius:sender.value];
}

- (void)_addCircleOnCurrentLocationWithRadius:(CGFloat)radius {
    MKCircle *circle = [MKCircle circleWithCenterCoordinate:_annotation.coordinate radius:radius];
    [self.mapView addOverlay:circle level:MKOverlayLevelAboveRoads];
}

#pragma mark - MKMapViewDelegate
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    MKPinAnnotationView *view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Annotation"];
    return view;
}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay {
    MKCircleView *view = [[MKCircleView alloc] initWithCircle:overlay];
    view.fillColor = [UIColor redColor];
    view.strokeColor = [UIColor blueColor];
    view.alpha = 0.3;
    return view;
}

@end

I've tried:

  • removing old overlays and adding new one,
  • doing as above with NSOperationQueue

Here is a screen recording how it looks like.

I see that is possible to do, Apple did this in Reminders app.

Also I'm familiar with following topics:

Thank you in advance.

Edit I've did it. I will add an answer with class which supports resizing today or tomorrow.


Solution

  • I've did it by creating subclass of MKCircleView and override - (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)ctx method. TSCircleView class is shared on github here..