【问题标题】:Reliably retrieve MKAnnotationView for MKAnnotation可靠地为 MKAnnotation 检索 MKAnnotationView
【发布时间】:2017-02-09 18:12:27
【问题描述】:

我可以通过简单地将annotation 传递给MKMapView 来获得annotationView

view(for annotation: MKAnnotation) -> MKAnnotationView?

这会返回一个optional,因为如果注释不在屏幕上,它可能没有关联的annotationView。

是否有可靠的方法来始终为给定的MKAnnotation 检索MKAnnotationView

我什至尝试过手动调用 MKMapView 的委托,但它使我的应用程序崩溃并显示一条模糊的消息:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
        return nil
    }

    return MKPinAnnotationView(annotation: annotation, reuseIdentifier: nil)
}

用例:

我正在开发一个自定义 VoiceOver 转子,它允许用户通过轻弹屏幕从一个大头针导航到另一个大头针。因此,如果每个大陆上都有 1 个大头针,他们可以向上或向下滑动,地图就会以该大头针为中心,而无需平移地图。困难是我需要告诉自定义转子去哪里,但是如果注释在屏幕外,还没有与之关联的 annotationView,所以我无法正确指示转子。

【问题讨论】:

    标签: swift mkmapview mkannotation mkannotationview uiaccessibility


    【解决方案1】:

    在我的情况下,我不仅需要 MKAnnotationView,而且还需要将地图置于注释的中心,所以这对我有用:

    // imperative that this is called without animation.
    // setCenter will trigger the delegate method mapView(_:viewFor:) immediately
    self.mapView.setCenter(requestedAnnotation.coordinate, animated: false)
    
    // by the time we need the view, there's one associated with the annotation
    if let annotationView = self.mapView.view(for: requestedAnnotation) {
      // we have an annotationView
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-05
      • 2012-06-08
      • 2015-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-23
      相关资源
      最近更新 更多