Implementé una anotación personalizada derivada de MKAnnotation llamada ContainerAnnotation y una vista de anotación personalizada derivada de MKAnnotationView con un método drawRect: ContainerAnnotationView. Por alguna razón, el método drawRect: no se está llamando y no puedo entender por qué.MKAnnotationView drawRect: no se llama
Aquí está el código fuente para mi vista de anotación.
ContainerAnnotationView.h:
@interface ContainerAnnotationView : MKAnnotationView
{
}
@end
ContainerAnnotationView.m:
@implementation ContainerAnnotationView
- (void) drawRect: (CGRect) rect
{
// Draw the background image.
UIImage * backgroundImage = [UIImage imageNamed: @"container_flag_large.png"];
CGRect annotationRectangle = CGRectMake(0.0f, 0.0f, backgroundImage.size.width, backgroundImage.size.height);
[backgroundImage drawInRect: annotationRectangle];
// Draw the number of annotations.
[[UIColor whiteColor] set];
UIFont * font = [UIFont systemFontOfSize: [UIFont smallSystemFontSize]];
CGPoint point = CGPointMake(2, 1);
ContainerAnnotation * containerAnnotation = (ContainerAnnotation *) [self annotation];
NSString * text = [NSString stringWithFormat: @"%d", containerAnnotation.annotations.count];
[text drawAtPoint: point withFont: font];
}
@end
Desde mi controlador de vista:
- (MKAnnotationView *) mapView: (MKMapView *) mapView viewForAnnotation: (id <MKAnnotation>) annotation
{
if ([annotation isKindOfClass: [ContainerAnnotation class]])
{
ContainerAnnotationView * annotationView = (ContainerAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier: _containerMapAnnotationId];
if (annotationView == nil)
{
annotationView = [[[ContainerAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: _containerMapAnnotationId] autorelease];
annotationView.centerOffset = CGPointMake(0, -17.5);
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType: UIButtonTypeDetailDisclosure];
annotationView.canShowCallout = YES;
}
annotationView.annotation = annotation;
return annotationView;
}
// etc...
}
tengo otras anotaciones que utilizan una v Anilla MKAnnotación con una imagen que funciona bien. También tengo otra vista de anotación personalizada que no implementa drawRect: funciona bien. ¿Alguna idea de lo que estoy haciendo mal aquí?
¿Cómo se hace visible la vista? ¿Se hace visible después de mapView: viewForAnnotation: returns? –
Lo pido porque no veo ningún código para las otras vistas de anotación que explícitamente los hacen visibles. En ninguna parte llaman setNeedsDisplay. –
Esto es todo lo que necesitaba para hacerlo funcionar. – VaporwareWolf