Allow tapping anywhere on an annotation callout without a callout accessory view

前端 未结 5 1532
情歌与酒
情歌与酒 2020-12-28 16:35

I have a map view that adds annotations more or less like this:

- (MKAnnotationView *)mapView:(MKMapView *)mapView
            viewForAnnotation:(id 

        
5条回答
  •  孤城傲影
    2020-12-28 17:22

    To tap the callout button after the user has clicked on the Annotation view, add a UITapGestureRecognizer in didSelectAnnotationView. This way you can implement tap on the callout without needing the accessory views.

    You can then get the annotation object back from the sender for further action.

    - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
    {
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(calloutTapped:)];
        [view addGestureRecognizer:tapGesture];
    }
    
    -(void)calloutTapped:(UITapGestureRecognizer *) sender
    {
        NSLog(@"Callout was tapped");
    
        MKAnnotationView *view = (MKAnnotationView*)sender.view;
        id  annotation = [view annotation];
        if ([annotation isKindOfClass:[MKPointAnnotation class]])
        {
            [self performSegueWithIdentifier:@"annotationDetailSegue" sender:annotation];
        }
    }
    

提交回复
热议问题