MKUserLocation是可选择的,错误地拦截来自自定义MKAnnotationViews的触摸 [英] MKUserLocation is selectable, falsely intercepting touches from custom MKAnnotationViews

查看:234
本文介绍了MKUserLocation是可选择的,错误地拦截来自自定义MKAnnotationViews的触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的iOS应用程序中有一个普通的地图,其中显示用户位置已启用 - 这意味着我在地图上有我的正常蓝点,显示我的位置和准确度信息。标注在代码中被禁用。

I have a normal map in my iOS app where "Shows Users Location" is enabled - meaning I have my normal blue dot on the map, showing my position and accuracy info. The callout is disabled in code.

但我也有自定义的MKAnnotationViews,它们在地图上绘制,所有这些都有自定义标注。

But I also have custom MKAnnotationViews that are plotted around the map which all have custom callouts.

这个工作正常,但问题是当我的位置在MKAnnotationView的位置时,蓝点(MKUserLocation)拦截触摸,因此MKAnnotationView不会触摸不到。

This is working fine, but the problem is that when my location is on the location of a MKAnnotationView, the blue dot (MKUserLocation) intercepts the touch, so the MKAnnotationView doesn't get touched.

如何禁用蓝点上的用户交互,以便MKAnnotationViews而不是蓝点拦截触摸?

How can I disable the user interaction on the blue dot so that the touches are intercepted by the MKAnnotationViews and not the blue dot?

这是我到目前为止所做的:

This is what I do so far:

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation;
{
    if (annotation == self.mapView.userLocation)
    {
        [self.mapView viewForAnnotation:annotation].canShowCallout = NO;
        return [self.mapView viewForAnnotation:annotation];
    } else {
        ...
    }
}


推荐答案

禁用标注不会禁用视图上的触摸( didSelectAnnotationView 仍会被调用)。

Disabling the callout doesn't disable touches on the view (didSelectAnnotationView will still get called).

要在注释视图上禁用用户交互,请将其启用属性设置为

To disable user interaction on an annotation view, set its enabled property to NO.

然而,不是将启用设置为 viewForAnnotation 委托方法中,我建议在 didAddAnnotationViews 委托方法中以及 viewForAnnotation ,只需为 MKUserLocation 返回 nil

However, instead of setting enabled to NO in the viewForAnnotation delegate method, I suggest doing it in the didAddAnnotationViews delegate method instead and in viewForAnnotation, just return nil for MKUserLocation.

示例:

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation;
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
    {
        return nil;
    }

    //create annotation view for your annotation here...
}

-(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
    MKAnnotationView *av = [mapView viewForAnnotation:mapView.userLocation];
    av.enabled = NO;  //disable touch on user location
}

这篇关于MKUserLocation是可选择的,错误地拦截来自自定义MKAnnotationViews的触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆