gpt4 book ai didi

ios - 如何在 map View 中仅对注释设置可访问性?

转载 作者:可可西里 更新时间:2023-11-01 04:45:39 26 4
gpt4 key购买 nike

我正在尝试使我的 map View 可访问,但这样做有一个问题:

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

   self.mapView.isAccessibilityElement=YES; 

然后, map View 不会被画外音朗读。

如果我这样设置:

  self.mapView.isAccessibilityElement=NO;

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

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

我还尝试为 map View 设置辅助功能元素:

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

但仍然没有运气。

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

最佳答案

我认为您遇到了困难,因为 MKMapView 是一个 UIAccessibilityContainer,所以 isAccessibilityElement 如果 默认为 false

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

这是一个很好的方法,因为它为用户提供了一种自定义的、特定于应用程序的方式来导航您的 map ,同时也不会影响 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]
}

关于ios - 如何在 map View 中仅对注释设置可访问性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31878463/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com