gpt4 book ai didi

iphone - MKAnnotationView绘制矩形: not getting called

转载 作者:行者123 更新时间:2023-12-03 18:28:07 24 4
gpt4 key购买 nike

我已经实现了一个从 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com