我创建了一个NSMutableArray,其中包含从符合MKAnnotation协议的类派生的对象。我已经设置了注释的标题和副标题,并成功地将它们添加到了MKMapView中,使用以下代码:
[[self customMapView] addAnnotations:locationArray];
我现在想做的是使大头针动态掉落,最初我认为可以使用选项1来实现,虽然这样一切都正常,但大头针并没有动态掉落。
// Option1
// WORKS FOR: pinColor YES, animatesDrop NO, LABEL YES
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
for(MKPinAnnotationView *eachView in views) {
[eachView setAnimatesDrop:YES];
[eachView setPinColor:MKPinAnnotationColorPurple];
}
}
我的下一个猜想是尝试Option2,这似乎非常有效,但我有两个问题。 标题和副标题不显示,我的自定义注释对象被传递进去了(我可以在调试器中看到它),但其中包含的信息没有传递到新的pin
。其次,这会创建一组新的MKAnnotationViews,旧的MKAnnotationViews会发生什么情况?会有任何内存泄漏的问题吗?
// Option2
//FOR: pinColor YES, animatesDrop YES, LABEL NO
- (MKAnnotationView *) mapView: (MKMapView *) mapView viewForAnnotation: (id<MKAnnotation>) annotation {
MKPinAnnotationView *pin = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier: @"annotation_ID"];
if (pin == nil) {
pin = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"annotation_ID"] autorelease];
} else {
pin.annotation = annotation;
}
pin.pinColor = MKPinAnnotationColorRed;
pin.animatesDrop = YES;
return pin;
}
编辑:我通过添加pin.canShowCallout = YES;
解决了标题和副标题消失的问题。