iphone - MKAnnotationView绘制矩形: not getting called

标签 iphone drawrect mkannotationview

我已经实现了一个从 MKAnnotation 派生的自定义注释(称为 ContainerAnnotation),以及一个从 MKAnnotationView 派生的自定义注释 View ,其中包含名为 ContainerAnnotationView 的 drawRect: 方法。由于某种原因,drawRect: 方法没有被调用,我不明白为什么。

这是我的注释 View 的源代码。

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

从我的 View Controller :

- (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 和图像,效果很好。我还有另一个自定义注释 View ,它没有实现drawRect:效果很好。知道我在这里做错了什么吗?

最佳答案

问题原来是我的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;
}

关于iphone - MKAnnotationView绘制矩形: not getting called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3602439/

相关文章:

iphone - strong(在 LLVM 中)和 retain(在 GCC 中)有什么区别?

iphone - drawRect Circle 在圆圈内用于显示进度

cocoa - NSScrollview 和透明、覆盖 NSScroller 子类

ios - 从另一个 View Controller 中的类快速绘制形状(以编程方式)

ios - 不是地理 map 的 map 上的 AnnotationView

ios - 如何创建符合 <MKAnnotation> 协议(protocol)的自定义类

ios - MKAnnotationView 框架的初始化程序

ios - 从所选单元格的照片库中获取图像问题?

iphone - 如何在 iPhone 4 上实现频闪灯

ios - 我在 Firebase 中看到应用内购买,但在 iTunes connect/AppAnnie 中看不到它们