【问题标题】:How to make whole call out tappable?如何使整个呼出可点击?
【发布时间】:2016-03-08 04:53:33
【问题描述】:

我在地图上添加了注释并添加了标题、leftCalloutAccessoryView (ImageView) 和 rightCalloutAccessoryView (UIButton),当点击按钮时它会播放音频,我想要而不是点击按钮,而不是点击按钮,当我将在它开始播放的任何区域上标注时音频。

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    
}

// 播放按钮方法:

 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     
}

提前致谢。

【问题讨论】:

    标签: ios swift annotations mkmapview callout


    【解决方案1】:

    添加手势识别器...未经测试,让我知道它是否有效?我使用了双击,因为单发太容易触发...

    请注意,tapGestureRecognizer 最终可能需要成为类变量。

    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapped:")
    tapGestureRecognizer.numberOfTapsRequired  = 2
     annotationView.addGestureRecognizer(tapGestureRecognizer)
    
    func tapped(sender: UITapGestureRecognizer)
    {
        print("tapped ",sender)
        // play sound
    }
    

    【讨论】:

    • 它工作但有一个问题。当我在显示标注之前点击注释时,此方法也在调用。
    • 看@“发件人”,也许你可以根据它找出呼叫哪个。
    • 或者最好还是尝试使用长按而不是点击。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2019-08-10
    • 2015-06-16
    • 2018-01-01
    • 2016-04-25
    • 1970-01-01
    相关资源
    最近更新 更多