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

前端 未结 5 1533
情歌与酒
情歌与酒 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:06

    Dhanu A's solution in Swift 3:

    func mapView(mapView: MKMapView, didSelectAnnotationView view:MKAnnotationView) {
        let tapGesture = UITapGestureRecognizer(target:self,  action:#selector(calloutTapped(sender:)))
        view.addGestureRecognizer(tapGesture)
    }
    
    func mapView(mapView: MKMapView, didDeselectAnnotationView view: MKAnnotationView) {
        view.removeGestureRecognizer(view.gestureRecognizers!.first!)
    }
    
    func calloutTapped(sender:UITapGestureRecognizer) {
        let view = sender.view as! MKAnnotationView
        if let annotation = view.annotation as? MKPointAnnotation {
            performSegue(withIdentifier: "annotationDetailSegue", sender: annotation)
        }
    }
    
    0 讨论(0)
  • 2020-12-28 17:09
    - (void)mapView:(MKMapView *)mv annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
    {
        Park *parkAnnotation = (Park *)[view annotation];
        switch ([control tag]) {
            case 0: // left button
            {
                NSURL *url = [NSURL URLWithString:parkAnnotation.link];
                [[UIApplication sharedApplication] openURL:url];
            }
                break;
    
            case 1: // right button
            {
                // build a maps url. This will launch the Maps app on the hardware, and the apple maps website in the simulator
                CLLocationCoordinate2D coordinate = self.locationManager.location.coordinate;
                NSString *url2 = [NSString stringWithFormat:@"http://maps.apple.com/maps?saddr=%f,%f&daddr=%f,%f",coordinate.latitude,coordinate.longitude,parkAnnotation.location.coordinate.latitude,parkAnnotation.location.coordinate.longitude];
    
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url2]];
            }
                break;
    
            default:
                NSLog(@"Should not be here in calloutAccessoryControlTapped, tag=%ld!",(long)[control tag]);
                break;
        }
    }
    
    0 讨论(0)
  • 2020-12-28 17:16

    Try to set custom image for button without changing UIButtonTypeDetailDisclosure type.

    UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];        
    [detailButton setImage:[UIImage imageNamed:@"icon"] forState:UIControlStateNormal];
    

    For iOS7 and above this image will be tinted by default. If you want to keep original icon use the following

    [[UIImage imageNamed:@"icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
    

    Or if you want to remove icon at all

    [detailButton setImage:[UIImage new] forState:UIControlStateNormal];
    
    0 讨论(0)
  • 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 <MKAnnotation> annotation = [view annotation];
        if ([annotation isKindOfClass:[MKPointAnnotation class]])
        {
            [self performSegueWithIdentifier:@"annotationDetailSegue" sender:annotation];
        }
    }
    
    0 讨论(0)
  • 2020-12-28 17:24

    If you don't need tap indication, I'd advise to throw a UITapGestureRecognizer into the callout upon creation and add your handling object (perhaps controller) as a target with appropriate action.

    0 讨论(0)
提交回复
热议问题