How to make whole call out tappable?

这一生的挚爱 提交于 2019-12-08 12:04:09

问题


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
}


来源:https://stackoverflow.com/questions/35859244/how-to-make-whole-call-out-tappable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!