Подтвердить что ты не робот

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

От моего контроллера:

- (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: это работает нормально. Любая идея, что я здесь делаю неправильно?

4b9b3361

Ответ 1

Проблема оказалась в том, что мой метод 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;
}

Ответ 2

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