8

我在应用程序中有自定义注释引脚:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    return [kml viewForAnnotation:annotation type:state];
}

我返回自定义视图并为 Placemark 的 annotationView 制作 setImage,例如:

- (MKAnnotationView *)viewForAnnotation:(id <MKAnnotation>)point type:(int)state
{
    // Find the KMLPlacemark object that owns this point and get
    // the view from it.
    for (KMLPlacemark *placemark in _placemarks) {
        if ([placemark point] == point) 
        {
            UIButton *disclosureButton = [UIButton buttonWithType: UIButtonTypeDetailDisclosure]; 
            [[placemark annotationView] setCanShowCallout: YES];            
            [[placemark annotationView] setRightCalloutAccessoryView:disclosureButton];

            if (state == 0)
            {
                [[placemark annotationView] setImage:[UIImage imageNamed:@"ic_pin_tour.png"]];
            }
            else
            {
                [[placemark annotationView] setImage:[UIImage imageNamed:@"ic_pin_point.png"]];
            }

            return [placemark annotationView];
        }
    }
    return nil;
}

但是,如果我长按注释针,它的外观就会更改为默认视图(RedPin)。我不明白长按时调用了什么方法。我尝试使用 UITapGestureRecognizer,但没有发现。如果我只是点击注释针,一切正常,我的自定义注释针视图不会消失。您可以在此屏幕截图中看到我的意思: 非常有用的图像示例

那么,为什么长按时注释针的外观会发生变化?

4

1 回答 1

23

因此,如果您想为注释视图使用自定义图像,请始终使用通用 MKAnnotationView 而不是 MKPinAnnotationView。我只有一个地方有 MKPinAnnotationView,当我用 MKAnnotationView 替换它时,现在一切正常:

- (MKAnnotationView *)annotationView
{
    if (!annotationView) {
        id <MKAnnotation> annotation = [self point];
        if (annotation) {
            MKAnnotationView *pin =
                [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
            pin.canShowCallout = YES;
            annotationView = pin;
        }
    }
    return annotationView;
}
于 2011-11-24T13:19:40.570 回答