【问题标题】:Reusing custom drawn annotation view重用自定义绘制的注释视图
【发布时间】:2012-02-25 10:23:49
【问题描述】:

我已经设法在自定义彩色圆圈注释中绘制数字(基于this)。我想对我的自定义注释类进行一些优化,并阅读了有关重用的信息。 我的问题是,如果我让这些东西可重用,注释视图会在地图上混合,这是一个大问题。 自定义绘制的注释视图不能重复使用?还是它与视图的注释有某种关系?我的意思是,注释存储要在其视图上绘制的数字,实际上它是注释与其视图之间的一对一关系。

这是我的相关代码: 自定义注解视图的初始化:

-(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;
}

我尝试将self.image = 部分替换为自定义drawRect 函数,但结果相同。

谢谢。

【问题讨论】:

  • 或者,在这些情况下,只有彩色圆圈(圆圈的背景)可以重复使用,我应该将绘制的数字与背景分开?

标签: iphone ios mapkit mkannotation mkannotationview


【解决方案1】:

当 MKAnnotationView 出列时,会调用 prepareForReuse。在这种方法中,您可以检查内容是否需要重绘。

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

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

【讨论】:

  • 是的,这可能是最好的答案。毕竟我结束了将公共背景分离为可重用的,所以我将内容绘制在一个透明的圆圈上,然后放在重复使用的圆圈上。还是谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多