【问题标题】:UIButton as subview of MKAnnotationView not workingUIButton 作为 MKAnnotationView 的子视图不起作用
【发布时间】:2017-05-02 15:12:06
【问题描述】:

我正在使用 MKMapView 构建地图应用程序,但我似乎无法在 MKAnnotationView 的子视图中进行交互。

我有一个 UIView 类作为 MKAnnotationView 的主要子视图,在那里我有另一个子视图 UIButton,如下所示:

let button = UIButton(type: .infoLight)
button.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
button.addTarget(self, action: #selector(self.buttonTapped(sender:)), for: UIControlEvents.touchUpInside)
//self.bringSubview(toFront: button)
//button.isUserInteractionEnabled = true
self.addSubview(button)

(注释行似乎没有帮助)

那么类中的 tapped 函数是这样的:

func buttonTapped(sender: UIButton!) {
    print("button tapped:", sender)
}

知道为什么 buttonTapped() 方法不会触发吗?

【问题讨论】:

    标签: swift mkmapview mkannotationview


    【解决方案1】:

    我认为当 mapkit 将您的按钮添加到地图时,它会将其栅格化并将其添加为视图。所以“按钮”不再响应触摸事件。

    您可以使用 tapGestureRecognizer 来实现。

    当 mapkit 在以下函数中请求查看时。 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?

    使用添加了点击手势的 Uiview。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多