How to make whole call out tappable?

2019-06-11 05:00发布

问题:

I have added annotation on map and added title, leftCalloutAccessoryView (ImageView), and rightCalloutAccessoryView (UIButton), When tap on button it plays audio, I want instead of tapping on button when i will top on callout any area it starts playing audio.

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("AnnotationImage")
    if annotationView == nil {
        if annotation.isKindOfClass(Annotation) {

            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "AnnotationImage")             

            annotationView?.rightCalloutAccessoryView =   self.playButton() // Custome button for rightCallOut. 
            annotationView?.leftCalloutAccessoryView = UIImageView(image: UIImage(named: "annotationPinImage"))
            annotationView?.image = UIImage(named: "annotationPinImage.pdf")    
            annotationView?.canShowCallout = true   
            annotationView?.userInteractionEnabled = true    
    } else {    
         annotationView?.annotation = annotation       
    }   
    return annotationView    
}

// Play Button methods:

 func playButton() -> UIButton {    
    let image: UIImage = UIImage(named: "BtnPlayAudio.pdf")!    
    let button: UIButton = UIButton(type: .Custom)    
    button.frame = CGRectMake(0, 0, image.size.width, image.size.height)    
    button.setImage(image, forState: .Normal)     
    return button     
}

Thanks in advance.

回答1:

Add a gesture recogniser... untested, let me know if it works? I used a double tap cause a single will fire too easily...

Note the tapGestureRecognizer may need to be a class variable ultimately.

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapped:")
tapGestureRecognizer.numberOfTapsRequired  = 2
 annotationView.addGestureRecognizer(tapGestureRecognizer)

func tapped(sender: UITapGestureRecognizer)
{
    print("tapped ",sender)
    // play sound
}