ios - didSelect for MKAnnotationView 未触发

标签 ios swift mkmapview mapkit mkmapviewdelegate

我无法在可选择的 MapView 上创建自定义注释。我尝试了几种方法,但没有成功:

  • 使用 MKMapViewDelegate 的 didSelect
  • 使用添加到 AnnotationView 的 UIButton
  • 为 ImageView 使用 UITapGestureRecognizer,它应该用于注释

您可以在下面的代码中找到所有这些方法的注释:

override func viewDidLoad() {
    super.viewDidLoad()
    // some more initialization code

    for item in items {
        let annotation = IdentifiedMKPointAnnotation() // has only one additional property named "id"

        annotation.coordinate = CLLocationCoordinate2D(latitude: item.spots[0].latitude!, longitude: item.spots[0].longitude!)
        annotation.id = i

        self.mapView.addAnnotation(annotation)

        i += 1
    }
}


// MARK: - MapViewDelegate
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let item = self.items[(annotation as! IdentifiedMKPointAnnotation).id!]

    let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "")
    //annotationView.canShowCallout = false
    //annotationView.isEnabled = false

    let imageView = UIImageView()
    imageView.frame.size = CGSize(width: 40, height: 40)
    imageView.layer.cornerRadius = 20
    imageView.layer.masksToBounds = true
    imageView.af_setImage(withURL: URL(string: item.images.count > 0 ? item.images[0] : item.image!)!)
    imageView.contentMode = .scaleAspectFill

    //let gr = UIGestureRecognizer(target: self, action: #selector(annotationSelected(_:)))
    //imageView.addGestureRecognizer(gr)
    //imageView.isUserInteractionEnabled = true

    //let button = UIButton()
    //button.frame.size = CGSize(width: 40, height: 40)
    //button.setImage(#imageLiteral(resourceName: "play_arrow"), for: .normal)
    //button.addTarget(self, action: #selector(annotationSelected(_:)), for: .touchUpInside)

    annotationView.addSubview(imageView)
    //annotationView.addSubview(button)

    return annotationView
}

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    performSegue(withIdentifier: "showDetail", sender: self.items[(view.annotation as! IdentifiedMKPointAnnotation).id!])

    view.setSelected(false, animated: false)
}

func annotationSelected(_ sender: UIImageView) {
    performSegue(withIdentifier: "showDetail", sender: self.items[((sender.superview as! MKAnnotationView).annotation as! IdentifiedMKPointAnnotation).id!])
}

对于为什么 Action 没有/没有触发有什么想法吗?

最佳答案

如果注释的 title 为空,则不会调用 didSelect 委托(delegate)。试试下面的代码。

let annotation = IdentifiedMKPointAnnotation()
annotation.title = "title"

关于ios - didSelect for MKAnnotationView 未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45844181/

相关文章:

ios - 如何在没有 View Controller 的情况下将选项卡栏项添加到 UITabBarController?

ios - 我想知道为什么 UIImagePickerController 裁剪正方形有轻微的偏移,以及如何修复它

ios - Swift 4 - 使用 URLSession 获取上传图片进度

ios - Swift 1.2 - Xcode 6.4 - 如何使用 Parse.com 发送短信 (sms) 类似于发生了什么?

ios - MKOverlay 需要很长时间才能从异步调用中显示

ios - Restkit,停止记录?

ios - 在 iOS 上更改 AUGraph 的采样率

swift - 在 for 循环中使用延迟 -- Swift

ios - 按位置和缩放级别向服务器请求 mapkit 注释

ios - 带有集群 : how to display multiple annotations on same view 的 MapView