我已成功在自定义彩色圆圈注释内绘制数字(基于 this )。我想对我的自定义注释类进行一些优化,并且我阅读了有关重用的内容。 我的问题是,如果我使这些东西可重用,注释 View 会在 map 上混合,这是一个大问题。 自定义绘制的注释 View 不能重复使用?或者它与 View 的注释有某种关系吗?我的意思是,注释存储要在其 View 上绘制的数字,实际上注释与其 View 之间是一对一的关系。
这是我的相关代码: 自定义注释 View 的初始化:
-(id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier imageType:(int)imageType {
self = [super initWithAnnotation: annotation reuseIdentifier: reuseIdentifier];
if (self != nil)
{
if ([annotation isKindOfClass:[CircleMarker class]])
{
// custom annotation class with some extra fields
CircleMarker * clm = (CircleMarker * )annotation;
self.locationMarker = clm;
// ... setting frame and other stuff
self.image = [self getImage]; /* this method DRAWS image based on clm */
self.canShowCallout = NO;
}
...
}
代表:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
static NSString *reuseId_small = @"smallcircle";
static NSString *reuseId_big = @"bigcircle";
CircleAnnotationView * nca = nil;
if ((int)[self.mapView getZoomLevel] < ZOOM_LEVEL_FOR_NUMBERS)
{
nca = (CircleAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:reuseId_small];
if (nca == nil )
nca = [[[CircleAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId_small imageType:2] autorelease];
}
else
{
nca = (CircleAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:reuseId_big];
if ( nca == nil )
nca = [[[CircleAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId_big imageType:1] autorelease];
}
return nca;
}
我尝试用自定义 drawRect
函数替换 self.image =
部分,但结果是相同的。
谢谢。
最佳答案
当MKAnnotationView出队时,会调用prepareForReuse。在这种方法中,您可以检查内容是否需要重绘。
我在你的 CircleAnnotationView 代码中看到了这一点
关于iphone - 重用自定义绘制的注释 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9443171/