默认标注视图中的按钮停止响应触摸

时间:2019-03-19 03:59:50

标签: ios swift mkmapview mkannotation mkannotationview

所以我有一张带有大量注释的地图。每个注释都具有默认的标注视图,该视图附加了我创建的按钮,据称该按钮会将用户带到另一个视图控制器。首先,此按钮可以正常工作,但对于某些注释,除非我放大注释或再次单击注释,否则它不会注册触摸。我很失落。这是我的代码。

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        var annotationView = MKAnnotationView()
        guard let annotation = annotation as? LocationAnnotation else {return nil}
        var identifier = ""
        switch annotation.type {
        case .nightclub:
            identifier = "Nightclub"
        case .hookahLounge:
            identifier = "Hookah Lounge"
        case  .bar:
            identifier = "Bar"
        case .bowling:
            identifier = "Bowling Alley"
        case .arcade:
            identifier = "Arcade"
        case .pool:
            identifier = "Pool"
        }
        if let dequedView = mapSF.dequeueReusableAnnotationView(withIdentifier: identifier) {
            annotationView = dequedView
        } else {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        }
        annotationView.canShowCallout = true
        annotationView.isEnabled = true
        let button = UIButton()
        button.frame = CGRect(x: 0.0, y: 0.0, width: 30.0, height: 30.0)
        let image = UIImage(named: "go")
        button.setImage(image, for: .normal)
        annotationView.detailCalloutAccessoryView?.backgroundColor = UIColor.darkGray
        annotationView.rightCalloutAccessoryView = button
}

这是标注附件功能,该功能最初可以运行,但不能在随机时间运行。

      func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
             print("true button tapped")
    }

同样,我的问题是print语句不会针对各种注释执行。每当我按下标注中的按钮时,控制台都会打印出该语句,但在其他情况下则不会。我不知道为什么。任何帮助将不胜感激,因为这是我应用程序中最后剩下的错误之一。

1 个答案:

答案 0 :(得分:1)

这对我来说现在有点老了,但我找到了解决方法。为了获得对标注中按钮的触摸,我只需要添加一行annotationView.isUserInteractionEnabled = false。不论地图是否始终被放大,我在标注按钮中的图像都是交互式的。多么奇怪的情况,但是它畅通无阻。

相关问题