为什么自定义的 MKMapView 注释图像在触摸时会消失?

时间:2023-01-03
本文介绍了为什么自定义的 MKMapView 注释图像在触摸时会消失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在注释我的地图并设置图像就好了,但是当我点击 MapView 上的注释时,图像会从我的自定义图像返回到红色图钉.这是为什么呢?

I am annotating my map and setting an image just fine, but when I tap the annotation on the MapView, the image goes from my custom image back to the red pin. Why is this?

- (MKAnnotationView *)mapView:(MKMapView *)newMapView viewForAnnotation:(id )newAnnotation {
    MKPinAnnotationView *annotation = [[MKPinAnnotationView alloc] initWithAnnotation:newAnnotation reuseIdentifier:@"currentloc"];
    if (annotation == nil) {
        annotation = [[MKAnnotationView alloc] initWithAnnotation:newAnnotation reuseIdentifier:@"currentloc"];
    }

    annotation.image = [UIImage imageNamed:@"anno.png"];
    annotation.canShowCallout = YES;
    annotation.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bus_stop_30x30.png"]];
    annotation.leftCalloutAccessoryView = imgView;

    return annotation;
}

我的代码看起来与一些不会产生此问题的示例代码相同.

My code looks identical to some sample code that does not produce this problem.

推荐答案

在这里回答我自己的问题,以防其他人有同样的问题.请注意,我正在使用MKPinAnnotationView" - 它应该更改为MKAnnotationView"并且一切正常.

Answering my own question here, just in case others have the same issue. Notice that I am using "MKPinAnnotationView" - it should be changed to "MKAnnotationView" and everything works.

固定代码:

- (MKAnnotationView *)mapView:(MKMapView *)newMapView viewForAnnotation:(id )newAnnotation {
    MKAnnotationView *annotation = [[MKAnnotationView alloc] initWithAnnotation:newAnnotation reuseIdentifier:@"currentloc"];

    if (annotation == nil) {
        annotation = [[MKAnnotationView alloc] initWithAnnotation:newAnnotation reuseIdentifier:@"currentloc"];
    }

    annotation.image = [UIImage imageNamed:@"anno.png"];
    annotation.canShowCallout = YES;
    annotation.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bus_stop_30x30.png"]];
    annotation.leftCalloutAccessoryView = imgView;

    return annotation;
}

这篇关于为什么自定义的 MKMapView 注释图像在触摸时会消失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:新的@SystemApi 注解是什么意思,和@hide 有什么区别? 下一篇:在这种情况下 mapView:viewForAnnotation: 会被调用吗?

相关文章

最新文章