我的 SwiftUI 列表处于编辑模式(editMode == .active),并在某些列表行上使用 onDelete 修饰符。我想让 onDelete 操作具有可访问性(VoiceOver)。我该怎么做?
无需任何附加代码,删除列表元素上的附件(带减号的红色圆圈)对于辅助功能来说是不可见的(滑动辅助功能元素只会跳过此附件)。
我认为
onDelete
专门用于滑动操作。因此,对于accessibilityAction
,我需要添加一个额外的删除功能。
示例:
struct ContentView: View {
@State private var items = ["Item 1", "Item 2", "Item 3"]
var body: some View {
List {
ForEach(items.indices, id: \.self) { index in
Text(items[index])
.accessibility(label: Text("Item \(items[index])"))
.accessibility(hint: Text("Double-tap to delete"))
.accessibilityAction {
deleteItemFromVoiceOver(at: index)
}
}
.onDelete(perform: deleteItemFromSwipe)
}
}
func deleteItemFromSwipe(at offsets: IndexSet) {
// for swipe action
items.remove(atOffsets: offsets)
}
func deleteItemFromVoiceOver(at index: Int) {
// for VoiceOver
items.remove(at: index)
}
}