MKAnnotationView drawRect: не вызывается

Я реализовал пользовательскую аннотацию, полученную из MKAnnotation с именем ContainerAnnotation, и пользовательское представление аннотации, полученное из MKAnnotationView, с drawRect: метод с именем ContainerAnnotationView. По какой-то причине метод drawRect: не вызывается, и я не могу понять, почему.

Вот исходный код моего представления аннотаций.

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

From Мой контроллер вида:

- (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...
}

У меня есть другие аннотации, в которых используется ванильный MKAnnotation с изображением, которое работает нормально. У меня также есть другой пользовательский вид аннотации, который не реализует drawRect: он отлично работает. Любая идея, что я делаю здесь не так?

11
задан Charles 11 February 2012 в 23:29
поделиться

2 ответа

Проблема оказалась в том, что мой метод drawRect: никогда не вызывался, потому что фрейм не был установлен на ненулевой размер. Добавление метода initWithAnnotation: для этого решило проблему.

- (id) initWithAnnotation: (id <MKAnnotation>) annotation reuseIdentifier: (NSString *) reuseIdentifier
{
    self = [super initWithAnnotation: annotation reuseIdentifier: reuseIdentifier];
    if (self != nil)
    {
        self.frame = CGRectMake(0, 0, 30, 30);
        self.opaque = NO;
    }
    return self;
}
23
ответ дан 3 December 2019 в 04:11
поделиться

Вы где-нибудь вызывали setNeedsDisplay для этого подкласса представления? (Сразу после того, как вы сделаете этот вид видимым, это хорошее место.)

2
ответ дан 3 December 2019 в 04:11
поделиться
Другие вопросы по тегам:

Похожие вопросы: