iphone - 重用自定义绘制的注释 View

标签 iphone ios mapkit mkannotation mkannotationview

我已成功在自定义彩色圆圈注释内绘制数字(基于 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。在这种方法中,您可以检查内容是否需要重绘。

https://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKAnnotationView_Class/Reference/Reference.html

我在你的 CircleAnnotationView 代码中看到了这一点

关于iphone - 重用自定义绘制的注释 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9443171/

相关文章:

ios - 是否可以从共享扩展执行 API 调用

ios - NEHotspotConfigurationManager 没有密码/SSID 无效的错误

swift - 从 map 中删除注释 - Swift

ios - 调整 MKMapView 大小后的 zoomScale 不等于绘制 MKOverlay 时的 zoomScale

ios - 如何在推送表格 View 单元格时手动隐藏它?

arrays - 从 plist 加载时基于 MapKit 的应用程序崩溃

iphone - 逐像素检查 UIImage

iphone - 淡入/淡出整个 View ,一个 subview 除外

iphone - 卡号前 12 位数字应安全输入,其余 4 位数字正常

android - Css 背景图像无法在移动设备上正确显示