MKAnnotationView disappearing on swipe and double-tap zoom
Asked Answered
S

1

6

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));
}
Suzysuzzy answered 17/2, 2010 at 6:53 Comment(4)
also seeing this behavior, looking for a solution…Procter
even I am facing same problem , any luck ?Houstonhoustonia
is the drawRect called at all in the "non" working group?Maureenmaureene
I'm having the same problem, only my views don't implement drawRect, and they dissappear UNLESS the mapview coasts to a stop!Substantial
R
2

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

Rupertruperta answered 10/3, 2011 at 14:55 Comment(4)
The weather annotations in the sample code disappear and re-appear after every drag/ zoom.Entreaty
That's because they implemeted it that way, see '- (void)mapView:(MKMapView *)map regionDidChangeAnimated:(BOOL)animated' method in 'MapViewController.m'Rupertruperta
Can you please let me know where should I put this code? As your provided link is not available anymore.Cormack
Sure, it should be in your custom subclass of MKAnnotationView as indicated in the question. Seems Apple has removed their sample code, but I found it on github: github.com/acekiller/iOS-Samples/blob/master/WeatherMap/Classes/…Rupertruperta

© 2022 - 2024 — McMap. All rights reserved.