如何在行的滑动操作配置中为VoiceOver添加辅助function标签?

我正在使用Swift 4创建一个iOS应用程序而我没有使用Storyboard。 要从表视图控制器中删除行,用户将向左滑动该行,然后单击“删除”按钮。

这是我用来实现的代码(没有使用外部库):

override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { self.isAccessibilityElement = true self.accessibilityLabel = "Delete row" let deleteAction = UIContextualAction(style: .normal , title: "DELETE") { (action, view, handler) in self.removeRowFromMyList(indexPath: indexPath.row) MyListController.stations.remove(at: indexPath.row) self.tableView.deleteRows(at: [indexPath], with: .automatic) self.tableView.setEditing(false, animated: true) self.tableView.reloadData() } let swipeAction = UISwipeActionsConfiguration(actions: [deleteAction]) swipeAction.performsFirstActionWithFullSwipe = false return swipeAction } 

我确实检查了其他问题,但没有人提出这个问题。 如果您需要了解解决此问题的任何其他信息,请随时在此处发表评论。 谢谢 :)

使用Apple的UIAccessibility中的辅助function自定义操作

您只需设置辅助function自定义操作:

 cell.accessibilityCustomActions = [UIAccessibilityCustomAction(name: "Delete", target: self, selector: #selector(theCustomAction))] @objc private func theCustomAction() -> Bool { //Do anything you want here return true } 

更新:

所以我确实重新创建了这个项目,但这次我使用的是Storyboards(我不是最后一次),我从Cocoapods导入了SwipeCellKit库 ,我按照他们的文档和VoiceOver完全正常工作,从中删除了一个单元格indexPath.row没问题。

  1. 当Voice Over打开并且UITableViewCell处于焦点时,Voice Over将宣布“ 向上或向下滑动以选择自定义动作,然后双击以激活

  2. 如果用户遵循上述指令,则用户将能够选择许多可用动作中的一个并双击以激活它

  3. 执行动作后,Voice Over将宣布“ 执行动作

注意:

  • 使用标准控件的优点是可访问性主要由您处理。
  • 您不必担心它会破坏iOS的新版本
  • 如果系统提供内置function,那么请使用它。

实现UIAccessibilityCustomAction类的成员将允许您向UITableViewCell添加其他function。 在cellForRowAt: IndexPath ,添加以下代码以将自定义操作附加到单元格。

 cell.isAccessibilityElement = true let customAction = UIAccessibilityCustomAction(name: "Delete Row", target: self, selector: #selector(deleteRowAction)) cell.accessibilityCustomActions = [selectAction, disclosureAction] 

与自定义操作关联的选择器function强大,但很难将参数作为指示单元格或索引路径的参数传递。 此外,可访问性焦点不会激活tableView的didSelectRowAt: IndexPath

这里的解决方案是找到VoiceOver辅助function焦点的位置并从单元格获取信息。 此代码可以包含在您的选择器中,如下所示。

 @objc func deleteRowAction() -> Bool { let focusedCell = UIAccessibilityFocusedElement(UIAccessibilityNotificationVoiceOverIdentifier) as! UITableViewCell if let indexPath = tableView?.indexPath(for: focusedCell) { // perform the custom action here using the indexPath information } return true }