MKAnnotationView disappears when swipe and double tap

I have subclassed MKAnnotationView to create an annotation that basically draws a circle around a point on the map using override drawRect. The circle is perfectly drawn in the following situations (in the simulator):

  • When loading a map view
  • When scrolling, but only when the swipe movement is stopped before touching (so that the card does not “take” after touching)
  • In zoom mode, press

The circle will disappear when one of the following occurs:

  • Swipe the screen where the “coast” map ends after touching.
  • 2x scaling

The circle will appear again if any of the actions in the "working" group is taken after its disappearance.

What could be the reason for this? I'm not an expert on drawing / rendering / layout (to be honest, I'm not a C or iPhone expert either).

Here is some slightly simplified code that works best for my subclass of MKAnnotationView:

- (void)drawRect:(CGRect)rect {
    // Drawing code
 [self drawCircleAtPoint:CGPointMake(0,0) withRadius:self.radiusInPixels andColor:self.circleAnnotation.color];
}


- (void)drawCircleAtPoint:(CGPoint)p withRadius:(int)r {
    CGContextRef contextRef = UIGraphicsGetCurrentContext();

    float alpha = 0.75;

    CGContextSetRGBFillColor(contextRef, 255, 0, 0, alpha);
    CGContextSetRGBStrokeColor(contextRef, 255, 0, 0, alpha);

    // Draw a circle (border only)
    CGContextStrokeEllipseInRect(contextRef, CGRectMake(0, 0, 2*r, 2*r));
}
+5
source share
1 answer

Have you added this method?

- (void)setAnnotation:(id <MKAnnotation>)annotation
{
    [super setAnnotation:annotation];
    [self setNeedsDisplay];
}

This is taken from an Apple sample application called WeatherMap, which was removed from the Apple Developer Center but can be found on github https://github.com/acekiller/iOS-Samples/blob/master/WeatherMap/Classes/WeatherAnnotationView.m

+2
source

All Articles