MKUserLocation is selectable, falsely intercepting touches from custom MKAnnotationViews

落爺英雄遲暮 提交于 2019-12-05 04:34:06

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.

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.

Example:

- (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
}

Swift 4.2 Example:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation)   ->     MKAnnotationView? {
    if annotation is MKUserLocation {
        return nil
    }
// Add custom annotation views here.
}

func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView])      {
    // Grab the user location annotation from your IB Outlet map view.
    let userLocation = mapView.view(for: mapView.userLocation)
    userLocation?.isEnabled = false
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!