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

Why does a custom MKMapView annotation image disappear on touch?(为什么自定义的 MKMapView 注释图像在触摸时会消失?)
本文介绍了为什么自定义的 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 注释图像在触摸时会消失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

iOS refresh annotations on mapview(iOS在mapview上刷新注释)
How to calculate distance from user location to annotation when user moves(用户移动时如何计算用户位置到注释的距离)
In which case that mapView:viewForAnnotation: will be called?(在这种情况下 mapView:viewForAnnotation: 会被调用吗?)
What#39;s the meaning of new @SystemApi annotation, any difference from @hide?(新的@SystemApi 注解是什么意思,和@hide 有什么区别?)
Retrofit @body with @multipart having Issue(使用 @multipart 改造 @body 有问题)
iPhone Map Kit cluster pinpoints(iPhone Map Kit 集群定位)