我想捕捉可访问性焦点的变化。
我搜索并尝试过accessibilityElementDidBecomeFocused
但光标改变后没有触发。我想向按钮添加值,并在光标更改后删除该值。
我的代码是这样的:
override func viewDidLoad() {
flashButton.accessibilityLabel = "Change the flash status"
flashButton.accessibilityTraits = [.button]
flashButton.accessibilityIdentifier = "flashButton"
}
@IBAction func flashButtonTapped(_ sender: Any) {
var currentFlashStatus = "off"
guard let device = AVCaptureDevice.default(AVCaptureDevice.DeviceType.builtInWideAngleCamera, for: AVMediaType.video, position: .back) else { return }
guard device.hasTorch else { return }
do {
try device.lockForConfiguration()
if device.torchMode == .on {
device.torchMode = .off
flashButton.image = UIImage(named: "flashOffIcon")
currentFlashStatus = "off"
} else {
do {
try device.setTorchModeOn(level: 1.0)
flashButton.image = UIImage(named: "flashOnIcon")
currentFlashStatus = "on"
} catch {
print(error)
}
}
device.unlockForConfiguration()
} catch {
print(error)
}
flashButton.accessibilityLabel = "Turned \(currentFlashStatus) the flash"
}
首先,它显示“更改闪光灯状态”,如果我双击它,它会显示“打开闪光灯状态”,这是正确的。
如果我改变光标并返回到闪光灯按钮,它应该会再次显示更改闪光灯状态。但它说“打开闪光灯状态”
我怎样才能做到这一点?
我搜索并尝试过accessibilityElementDidBecomeFocused
但光标改变后没有触发。
如果您想使用UIAccessibilityFocus
非正式协议方法,您必须直接在对象中重写它们,而不是在视图控制器中(see 这个答案)。创建一个子类UIButton
例如:
import UIKit
class FlashButton: UIButton {}
extension FlashButton {
override open func accessibilityElementDidBecomeFocused() {
// Actions to be done when the element did become focused.
}
}
如果我改变光标并返回到闪光灯按钮,它应该会再次显示更改闪光灯状态。
无论您选择闪光按钮多少次,在更改其值之前都应始终先读出所需的状态。
下面的代码中提供了一个示例:
class FlashButton: UIButton {
private var intStatus: Bool = false
private var a11yLabelStatus: String = "off"
var status: Bool {
get { return intStatus }
set(newValue) { intStatus = newValue }
}
}
extension FlashButton {
override open func accessibilityElementDidBecomeFocused() {
// Actions to be done when the element did become focused.
}
override open func accessibilityElementDidLoseFocus() {
// Actions to be done when the element did lose focus.
}
//This native a11y function replaces your defined IBAction to change and read out the flash status.
override func accessibilityActivate() -> Bool {
intStatus = (intStatus == true) ? false : true
a11yLabelStatus = (intStatus == true) ? "on" : "off"
UIAccessibility.post(notification: .announcement,
argument: "flash status is " + a11yLabelStatus)
return true
}
override var accessibilityLabel: String? {
get { return "the flash status is " + a11yLabelStatus }
set { }
}
override var accessibilityHint: String? {
get { return "double tap to change the value." }
set { }
}
}
我怎样才能实现这个目标?
要么你尝试UIAccessibilityFocus
非正式协议来捕捉焦点变化,或者您只需使用上面的代码片段更改辅助功能元素的状态:您可以将两者结合起来,这取决于您在编码环境中适应这些概念。 ;o)
如果还不够,请看一下这个网站专门面向 a11y 开发人员,其中提供代码片段和插图,帮助您找到适合您实施的另一种解决方案。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)