我在应用程序中有自定义注释图钉:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
return [kml viewForAnnotation:annotation type:state];
}
我返回自定义 View 并为 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;
}
但如果我长按我的注释图钉,它会将外观更改为其默认 View (RedPin)。 我无法理解长按调用的是什么方法。我试着玩 UITapGestureRecognizer,但没有发现。如果我只是点击注释图钉一切正常,我的自定义注释图钉 View 不会消失。 你可以在这张截图中看到我的意思:
那么,为什么长按时注释图钉的外观会发生变化?
最佳答案
因此,如果您想为注释 View 使用自定义图像,请始终使用通用 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;
}
关于iphone - 长按自定义注释图钉更改为默认红色图钉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8257203/