我有一个注释图钉,用于显示可以拖动的当前搜索位置。这个注释只是一个普通的 MKPointAnnotation,然后我在 viewForAnnotation 方法中更改图像。这很好,直到我开始拖动注释,之后注释变为默认的红色引脚并丢失我设置的自定义图像。
我创建并添加这样的注释:
MKPointAnnotation *userAnnotation = [[MKPointAnnotation alloc] init];
[userAnnotation setCoordinate:userCoordinate];
[userAnnotation setTitle:@"My Location"];
[userAnnotation setSubtitle:@"Drag to move - Press to reset"];
[self.mapView addAnnotation:userAnnotation];
然后我像这样设置annotationView:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
....
[annotationView setImage:[UIImage imageNamed:@"locationpin"]];
[annotationView setDraggable:YES];
[annotationView.layer setZPosition:999];
return annotationView;
我实现了方法 didChangeDragState: fromState: 像这样:
- (void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)annotationView
didChangeDragState:(MKAnnotationViewDragState)newState
fromOldState:(MKAnnotationViewDragState)oldState {
if (newState == MKAnnotationViewDragStateEnding) {
CLLocationCoordinate2D droppedAt = annotationView.annotation.coordinate;
self.mockLocation = [[CLLocation alloc] initWithLatitude:droppedAt.latitude longitude:droppedAt.longitude];
NSLog(@"%@ mock location set to %f %f", [self class], droppedAt.latitude, droppedAt.longitude);
[self plotStations];
}
}
我不记得 iOS 6 上的这个问题,但它肯定存在于 iOS 7 上。
那么,如何让它在拖动之前、期间和之后保留其图像?