Ios 长时间单击后,MKPointAnnotation将更改为红色pin

Ios 长时间单击后,MKPointAnnotation将更改为红色pin,ios,mapkit,mkannotationview,Ios,Mapkit,Mkannotationview,我在长时间点击MKPointAnnotation时发现了一些奇怪的行为 我创建的pin如下所示: MKPointAnnotation *activeTRPoint = [[MKPointAnnotation alloc] init]; CLLocation *coord = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude]; activeTRPoint.coordinate = coord.coordinate;

我在长时间点击
MKPointAnnotation
时发现了一些奇怪的行为

我创建的pin如下所示:

MKPointAnnotation *activeTRPoint = [[MKPointAnnotation alloc] init];
CLLocation *coord = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
activeTRPoint.coordinate = coord.coordinate;
activeTRPoint.title = @"Title";
activeTRPoint.subtitle = @"subtitle";
//Added tag
[map addAnnotation:activeTRPoint];
[[map viewForAnnotation:activeTRPoint] setTag:1];
UIImage *im = [UIImage imageNamed:@"pin_icon.png"];
[[map viewForAnnotation:activeTRPoint] setImage:im];
因此,当我长时间单击pin时,它会变为红色pin(默认)。你知道为什么会这样吗

更新:添加viewForAnnotation方法以供进一步调查

- (MKAnnotationView *) mapView:(MKMapView *)mapView1 viewForAnnotation:(id <MKAnnotation>) annotation
{
    if(annotation != map.userLocation){
        // This is not the users location indicator (the blue dot)
        MKAnnotationView *view = [map dequeueReusableAnnotationViewWithIdentifier:@"myAnnotationIdentifier"];
        BOOL isCustomPin = [subtitles containsObject:((MKPointAnnotation*)annotation).subtitle];
        if (!view && isCustomPin == NO) {
            // Creating a new annotation view, in this case it still looks like a pin
            MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotationIdentifier"];
            annView.pinColor = MKPinAnnotationColorGreen;
            view = annView;
            view.canShowCallout = YES; // So that the callout can appear
            UIButton *btnViewVenue = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            [btnViewVenue addTarget:self action:@selector(pinTouched:) forControlEvents:UIControlEventTouchUpInside];
            view.rightCalloutAccessoryView = btnViewVenue;
            view.enabled = YES;
            view.canShowCallout = YES;
            view.multipleTouchEnabled = NO;
        }else{
            UIImage *im = [UIImage imageNamed:@"pin_icon.png"];
            [view setImage:im];
        }
        return view;
    }else{
        mapView1.userLocation.title = [Lang get:@"USER_CURRENT_LOCATION"];
        mapView1.userLocation.subtitle = @"";
        [mapView1 setTag:999];
        return nil;
    }
}
-(MKAnnotationView*)映射视图:(MKMapView*)映射视图1视图用于注释:(id)注释
{
if(注释!=map.userLocation){
//这不是用户位置指示器(蓝点)
MKAnnotationView*视图=[map dequeueReusableAnnotationViewWithIdentifier:@“myAnnotationIdentifier”];
BOOL isCustomPin=[subtitles包含对象:((MKPointAnnotation*)annotation.subtitle];
如果(!view&&isCustomPin==否){
//创建新的注释视图时,在本例中它看起来仍然像一个图钉
MKPinAnnotationView*annView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:@“myAnnotationIdentifier”];
annView.pinColor=MKPinAnnotationColorGreen;
视图=视图;
view.canShowCallout=YES;//以便可以显示调用
UIButton*BTNViewVincement=[UIButton按钮类型:UIButtonTypedTailExposition];
[btnView场馆添加目标:自我操作:@selector(pinTouched:)for ControlEvents:UIControlEventTouchUpInside];
view.rightCalloutAccessoryView=btnView场馆;
view.enabled=是;
view.canShowCallout=是;
view.multipleTouchEnabled=否;
}否则{
UIImage*im=[UIImage ImageName:@“pin_icon.png”];
[查看设置图像:im];
}
返回视图;
}否则{
mapView1.userLocation.title=[Lang get:@“用户当前位置”];
mapView1.userLocation.subtitle=@”;
[mapView1 setTag:999];
返回零;
}
}

在所示的
注释视图
委托方法中,实际创建的注释视图(alloc+init)的唯一类型是
MKPinAnnotationView

isCustomPin
YES
时,代码as从不实际上创建了一个
MKAnnotationView
,因此最终引用了一个
视图

  • nil
    ,因为
    dequeue
    没有返回任何内容,或者
  • 是一个
    MKPinAnnotationView
    ,因为这是唯一创建的视图类型,并且出列返回以前使用过的视图
所以当
isCustomPin
YES
时:

  • 它返回一个
    nil
    视图,地图视图将其解释为“显示默认红色pin”(基本上是一个红色
    MKPinAnnotationView
    ),或者
  • 它返回一个绿色的
    MKPinAnnotationView
在任何情况下,代码总是返回一个
MKPinAnnotationView
,对于该视图,地图视图通常会忽略
image
属性,并根据
pinColor
显示默认的pin图像


要解决此问题,必须将“自定义图像”管脚的创建和重复使用与“绿色管脚”分开。例如:

BOOL isCustomPin = [subtitles containsObject:((MKPointAnnotation*)annotation).subtitle];

if (isCustomPin)
{
    //Put dequeue and alloc+init of MKAnnotationView here.
    //NOTE: Use a DIFFERENT re-use identifier for "custom image" pins.
    //Eg. Use "myCustomIdentifier" -- not "myAnnotationIdentifier"
}
else
{
    //Put dequeue and alloc+init of MKPinAnnotationView here.
    //NOTE: Use a DIFFERENT re-use identifier from "custom image" pins.
    //That is, use "myAnnotationIdentifier" -- not "myCustomIdentifier".
}

一些单独的、可能与联合国有关的问题:

  • 用于确定
    isCustomPin
    的逻辑看起来有问题。最好创建一个自定义注释类并检查
    annotation
    是否属于该类型,而不是在某些外部数组中查找注释
  • 注释视图出列后,应将视图的
    注释
    属性设置为当前的
    注释
    ,否则视图将使用以前的注释属性
  • 应该没有必要使用标签(我不推荐)。用户位置注释可以通过其类
    MKUserLocation
    来识别,并且在
    pintouch:
    方法中,可以使用
    MKMapView
    selectedAnnotations
    属性来获取所选注释


顺便说一下,在调用
addAnnotation

之后,您还应该删除图像的设置。您是否实现了viewForAnnotation委托方法?如果是,请显示该代码。如果不是,那就是问题的一部分。谢谢你的回复。我在viewForAnnotation中更新了我的问题,代码创建了绿色的MKPinAnnotationViews,但在调用addAnnotation后,它指定了一个自定义图像。这是为什么?对不起,这是错误的代码。我更新了我的问题。在我的例子中,isCustomPin=YES;