【问题标题】:How to set accessibility only to annotations in map view?如何仅将可访问性设置为地图视图中的注释?
【发布时间】:2015-10-30 22:36:09
【问题描述】:

我正在尝试使我的地图视图可访问,但这样做有问题:

如果我尝试使 mapView 可访问,通过这样做:

   self.mapView.isAccessibilityElement=YES; 

然后,地图视图不会被画外音朗读。

如果我这样设置:

  self.mapView.isAccessibilityElement=NO;

然后画外音正在阅读地图中的所有内容、街道、建筑物、我的当前位置和我的注释。

我已经为我的注释提供了可访问性标签和提示,但我没有为地图视图提供任何其他值。

我还尝试通过设置地图视图的可访问性元素:

  [self.mapView setAccessibilityElements:@[self.mapView.annotations,self.mapView.userLocation]];

但还是没有运气。

有没有办法让语音只阅读注释而忽略街道、建筑物等剩余元素?

【问题讨论】:

    标签: ios mkmapview uiaccessibility


    【解决方案1】:

    我认为您遇到了困难,因为 MKMapView 是 UIAccessibilityContainer,因此默认情况下 isAccessibilityElement 如果 false

    您应该考虑在转子上制作自定义语音,允许用户仅通过您的注释来导航您的应用程序。

    这是一个很好的方法,因为它为用户提供了一种自定义的、特定于应用程序的地图导航方式,同时也不会影响MKMapView 的内置可访问性。

    网上几乎没有任何关于创建自定义转子的详细示例,但我刚刚成功创建了一个完全符合您需要的示例。我关注了WWDC Session 202(24:17 开始)。

    这是我的代码:

    func configureCustomRotors() {
      let favoritesRotor = UIAccessibilityCustomRotor(name: "Bridges") { predicate in
        let forward = (predicate.searchDirection == .next)
    
        // which element is currently highlighted
        let currentAnnotationView = predicate.currentItem.targetElement as? MKPinAnnotationView
        let currentAnnotation = (currentAnnotationView?.annotation as? BridgeAnnotation)
    
        // easy reference to all possible annotations
        let allAnnotations = self.mapView.annotations.filter { $0 is BridgeAnnotation }
    
        // we'll start our index either 1 less or 1 more, so we enter at either 0 or last element
        var currentIndex = forward ? -1 : allAnnotations.count
    
        // set our index to currentAnnotation's index if we can find it in allAnnotations
        if let currentAnnotation = currentAnnotation {
          if let index = allAnnotations.index(where: { (annotation) -> Bool in
            return (annotation.coordinate.latitude == currentAnnotation.coordinate.latitude) &&
        (annotation.coordinate.longitude == currentAnnotation.coordinate.longitude)
            }) {
              currentIndex = index
          }
        }
    
        // now that we have our currentIndex, here's a helper to give us the next element
        // the user is requesting
        let nextIndex = {(index:Int) -> Int in forward ? index + 1 : index - 1}
    
        currentIndex = nextIndex(currentIndex)
    
        while currentIndex >= 0 && currentIndex < allAnnotations.count {
          let requestedAnnotation = allAnnotations[currentIndex]
    
          // i can't stress how important it is to have animated set to false. save yourself the 10 hours i burnt, and just go with it. if you set it to true, the map starts moving to the annotation, but there's no guarantee the annotation has an associated view yet, because it could still be animating. in which case the line below this one will be nil, and you'll have a whole bunch of annotations that can't be navigated to
          self.mapView.setCenter(requestedAnnotation.coordinate, animated: false)
          if let annotationView = self.mapView.view(for: requestedAnnotation) {
            return UIAccessibilityCustomRotorItemResult(targetElement: annotationView, targetRange: nil)
          }
    
          currentIndex = nextIndex(currentIndex)
        }
    
        return nil
      }
    
      self.accessibilityCustomRotors = [favoritesRotor]
    }
    

    【讨论】:

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