如何通过语音获取当前所选tableviewcell的IndexPath?

时间:2023-01-24 16:57:31

I'm using Voice Over in my application. I'm having hard time to figure out which table cell is currently selected when voice over is on. How can I know whenever user initiates single tap or navigate through any tableviewcell?

我在我的应用程序中使用Voice Over。当开启语音时,我很难弄清楚当前选择了哪个表格单元格。每当用户启动单击或浏览任何tableviewcell时,我怎么知道?

1 个解决方案

#1


1  

These are the things you can try:

这些是你可以尝试的东西:

  • use the UIAccessibilityFocusedElement global function
  • 使用UIAccessibilityFocusedElement全局函数
  • override accessibilityElementDidBecomeFocused and accessibilityElementDidLoseFocus on the cell
  • 覆盖单元格上的accessibilityElementDidBecomeFocused和accessibilityElementDidLoseFocus
  • observe the UIAccessibilityElementFocused notification in NotificationCenter in situations where you need it (e.g. when the view controller for the table in question is showing)
  • 在需要的情况下观察NotificationCenter中的UIAccessibilityElementFocused通知(例如,当显示相关表的视图控制器时)

Also what element will report focus will most probably depend on whether your UITableViewCell has isAccessibilityElement set to true or false.

另外,报告焦点的元素很可能取决于您的UITableViewCell是否将isAccessibilityElement设置为true或false。

While the above will probably help you with literally what you asked, it is also possible that your overall approach to accessibility in this situation might be wrong if you need the above information. If you share more info on the bigger picture / motivation what you are trying to achieve, it might turn out that the information about focused element might not be needed at all and that another solution is more proper.

虽然上述内容可能会帮助您完全按照您的要求提供帮助,但如果您需要上述信息,那么在这种情况下您的整体可访问性方法可能也是错误的。如果您分享更多关于您想要实现的更大图片/动机的信息,可能会发现根本不需要关注焦点元素的信息,而另一种解决方案更合适。

If what you need is to add a hint for swiping, you can simply set accessibilityHint on the proper element (if you set isAccessibilityElement = true on the whole cell, then set that on the whole cell, otherwise try setting it on the label that VoiceOver reads in the cell), e.g. when you configure the cell for display (usually in tableView(_:cellForRowAt:)). In such case, you will not need to observe which element is focused, and simply let VoiceOver read hint available on that particular element/cell.

如果您需要添加提示进行滑动,则只需在适当的元素上设置accessibilityHint(如果在整个单元格上设置isAccessibilityElement = true,则在整个单元格上设置,否则尝试在VoiceOver的标签上设置它)读取细胞),例如配置要显示的单元格时(通常在tableView(_:cellForRowAt :)中)。在这种情况下,您不需要观察哪个元素被聚焦,只需让VoiceOver读取该特定元素/单元格上的提示即可。

#1


1  

These are the things you can try:

这些是你可以尝试的东西:

  • use the UIAccessibilityFocusedElement global function
  • 使用UIAccessibilityFocusedElement全局函数
  • override accessibilityElementDidBecomeFocused and accessibilityElementDidLoseFocus on the cell
  • 覆盖单元格上的accessibilityElementDidBecomeFocused和accessibilityElementDidLoseFocus
  • observe the UIAccessibilityElementFocused notification in NotificationCenter in situations where you need it (e.g. when the view controller for the table in question is showing)
  • 在需要的情况下观察NotificationCenter中的UIAccessibilityElementFocused通知(例如,当显示相关表的视图控制器时)

Also what element will report focus will most probably depend on whether your UITableViewCell has isAccessibilityElement set to true or false.

另外,报告焦点的元素很可能取决于您的UITableViewCell是否将isAccessibilityElement设置为true或false。

While the above will probably help you with literally what you asked, it is also possible that your overall approach to accessibility in this situation might be wrong if you need the above information. If you share more info on the bigger picture / motivation what you are trying to achieve, it might turn out that the information about focused element might not be needed at all and that another solution is more proper.

虽然上述内容可能会帮助您完全按照您的要求提供帮助,但如果您需要上述信息,那么在这种情况下您的整体可访问性方法可能也是错误的。如果您分享更多关于您想要实现的更大图片/动机的信息,可能会发现根本不需要关注焦点元素的信息,而另一种解决方案更合适。

If what you need is to add a hint for swiping, you can simply set accessibilityHint on the proper element (if you set isAccessibilityElement = true on the whole cell, then set that on the whole cell, otherwise try setting it on the label that VoiceOver reads in the cell), e.g. when you configure the cell for display (usually in tableView(_:cellForRowAt:)). In such case, you will not need to observe which element is focused, and simply let VoiceOver read hint available on that particular element/cell.

如果您需要添加提示进行滑动,则只需在适当的元素上设置accessibilityHint(如果在整个单元格上设置isAccessibilityElement = true,则在整个单元格上设置,否则尝试在VoiceOver的标签上设置它)读取细胞),例如配置要显示的单元格时(通常在tableView(_:cellForRowAt :)中)。在这种情况下,您不需要观察哪个元素被聚焦,只需让VoiceOver读取该特定元素/单元格上的提示即可。