2011-04-07 72 views
2

取決於MKMapView的縮放級別我需要重新繪製自定義的MKAnnotationView。至於,我明白我必須做的映象控制器重繪MKAnnotationView

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated 

刪除的下一個方法,然後添加註釋這裏的MKMapView強制AnnotationView的閃爍。 如何以正確的方式做到這一點?

回答

3

您不需要刪除它並將其添加回來。只需在自定義註釋視圖上進行更改並致電setNeedsDisplay即可。例如:

@interface AnnotationClusterView : MKAnnotationView { 
@property (nonatomic, assign) int badgeNumber; 
} 
@implementation AnnotationClusterView 
@synthesize badgeNumber; 
// ... 
- (void)drawRect:(CGRect)rect { 
    NSString *string = [NSString stringWithFormat:@"%d",self.badgeNumber]; 
    [string drawInRect:stringRect withFont:[UIFont fontWithName:@"Helvetica-Bold" size:13.0]]; 
} 
@end 

當變焦的變化,得到了MKAnnotationView參考,設置不同的badgeNumber,並要求重繪調用[myView setNeedsDisplay];。你可以爲圖像做同樣的事情。

+0

感謝您的支持。我瘋了試圖找到我的自定義註釋視圖的問題。事實證明,在我爲現有視圖分配了一個新的註釋之後 - 它不會重新繪製視圖。使用SetNeededDisplay修復了這個問題。 – EasyCoder 2011-07-03 16:28:51

+0

確保你從mapView調用setNeedsDisplay,而不是從本身調用setNeedsDisplay,我花了整整一天的時間來調用'[self setNeedsDisplay];'不會使'drawRect:'執行。 – 2013-10-11 17:33:57