拖动引脚时,iOS 7注释引脚图像已更改

时间:2013-09-24 14:39:38

标签: ios objective-c annotations mapkit ios7

我有一个注释引脚,用于显示可以拖动的当前搜索位置。这个注释只是一个普通的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:like so:

- (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上。

那么,如何让它在拖动之前,期间和之后保留其图像?

1 个答案:

答案 0 :(得分:0)

我也遇到了这个问题。 我设法通过在viewForAnnotation上从“MKPinAnnotationView”更改为“MKAnnotationView”来解决这个问题。

这样的事情:

- (MKAnnotationView *) mapView: (MKMapView *) mapView viewForAnnotation: (id<MKAnnotation>) annotation
{
    MKAnnotationView *pin = (MKAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:@"my_pin"];
    if (pin == nil) {
        pin = [[MKAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier:@"my_pin"];

    } else {
        pin.annotation = annotation;
    }

    pin.draggable = YES;
    pin.image = [UIImage imageNamed:@"img_map_pin"];
    pin.selected = YES;

    return pin;
}

不同之处在于拖放不会显示引脚掉落的动画。

希望它有所帮助。