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

前端 未结 5 1551
情歌与酒
情歌与酒 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条回答
  •  旧时难觅i
    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)
        }
    }
    

提交回复
热议问题