大頭針視圖也存在重用機制,優化內存
類似于tableView一樣,設置代理,實現重用方法viewForAnnotation
設置大頭針視圖顏色需要使用MKAnnotationView的子類MKPinAnnotationView才行
為了不影響定位大頭針狀態,所以重用中需要判斷排除定位大頭針
定位大頭針類型(MKUserLocation)
/**
* 當設置大頭針視圖的時候大頭針模型時調用
*
* @param mapView 地圖視圖
* @param annotation 大頭針模型
*
* @return 大頭針視圖
*/
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
static NSString *identifier = @"annotation";
// 排除定位大頭針(否則定位大頭針樣式也會被修改掉)
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
// 設置顏色需要使用MKAnnotationView的子類才行 MKPinAnnotationView
MKPinAnnotationView *anno = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (anno == nil) {
anno = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:identifier];
}
// 設置屬性
anno.pinTintColor = [UIColor greenColor];
/**
* 以上設置后,大頭針視圖上方的標注還需要手動設置顯示
*/
// 設置顯示標注
anno.canShowCallout = YES;
// 設置動畫效果滑落
anno.animatesDrop = YES;
return anno;
}
演示效果(未設置顯示標注和動畫滑落):
pinTintColor.png
customer.png