2012-02-25 82 views
2

我已經設法在自定義彩色圓圈註釋中繪製數字(基於this)。我想對自定義註記類進行一些優化,然後閱讀關於重用的內容。 我的問題是,如果我讓東西可重用,註釋視圖混合在地圖上,這是一個大問題。 自定義繪製的註釋視圖不能重複使用?或者它與視圖的註釋有什麼關係?我的意思是,註釋存儲要在其視圖上繪製的數字,實際上它是註釋與其視圖之間的1to1關係。重複使用自定義繪製的註釋視圖

這裏是我的相關代碼: 定製annotationview的init:

-(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 =一部分,但結果是一樣的。

謝謝。

+0

或者裏面,在這些情況下,只有彩色圓圈(圓圈的背景),可重複使用,我應該繪製的數量從背景中分離出來? – Templar 2012-02-29 08:37:39

回答

2

當MKAnnotationView出列時,會調用prepareForReuse。在這種方法中,您可以檢查內容是否需要重繪。

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

我認爲這會你CircleAnnotationView代碼

+0

是的,可能這是最好的答案。畢竟我最終將共同的背景分離爲可重用的,所以我將這些內容繪製在一個透明的圓圈上,並放置在重複使用的圓圈上。不管怎麼說,還是要謝謝你。 – Templar 2012-03-16 12:01:41