我已經設法在自定義彩色圓圈註釋中繪製數字(基於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 =
一部分,但結果是一樣的。
謝謝。
或者裏面,在這些情況下,只有彩色圓圈(圓圈的背景),可重複使用,我應該繪製的數量從背景中分離出來? – Templar 2012-02-29 08:37:39