Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MKAnnotationView disappearing on swipe and double-tap zoom

I have subclassed MKAnnotationView to create an annotation that basically draws a circle around a point on a map view through override of drawRect. The circle draws fine in the following situations (in the simulator):

  • On initial load of the map view
  • On swipe, but only when swipe motion is stopped before touch ends (so that map doesn't "coast" after touch ends)
  • On pinch zoom

The circle will disappear when any of the following actions occur:

  • Swipe where map "coasts" after touch ends
  • Double-tap zoom

The circle will reappear if any of the actions in the "working" group are taken after it has disappeared.

What might cause this? I'm not a draw/display/layout expert (frankly, I'm not an obj C or iPhone expert either).

Here is some slightly simplified code that seems most relevant from my MKAnnotationView subclass:

- (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));
}
like image 936
Steve N Avatar asked Feb 17 '10 06:02

Steve N


1 Answers

Did you add this method?

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

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

like image 136
Dunja Lalic Avatar answered Nov 14 '22 23:11

Dunja Lalic