长按自定义注释标记后变为默认的红色标记

8

我在应用程序中有自定义的注释图钉:

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

我在这里返回自定义视图并为Placemark的注释视图设置setImage,例如:

我在这里返回自定义视图并为Placemark的注释视图设置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进行操作,但没有找到答案。如果我只是点击注释图钉,一切都正常,我的自定义注释图钉视图不会消失。您可以在此屏幕截图中看到我的意思:so useful image with example那么,为什么注释图钉在长按时会更改外观?
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;
}

1
运行得很好。事实是,MKPinAnnotation视图是MKAnnotation视图的子类,具有您不需要的功能。 - igraczech

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接