我想捕捉可访问性焦点的变化。
我搜索并尝试了 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"
}
首先,它会显示“更改闪光灯状态”,如果我双击它会显示“打开闪光灯状态”,这是正确的。
如果我更改光标并返回到闪光灯按钮,它应该说再次更改闪光灯状态。但是它说“打开闪光灯状态”
我怎样才能做到这一点?
最佳答案
I searched and tried
accessibilityElementDidBecomeFocused
but didn't trigger after cursor changed.
如果你想使用 UIAccessibilityFocus
非正式协议(protocol)方法,你必须直接在你的对象中覆盖它们,而不是在 View Controller 中(参见this answer)。创建 UIButton
的子类,例如:
import UIKit
class FlashButton: UIButton {}
extension FlashButton {
override open func accessibilityElementDidBecomeFocused() {
// Actions to be done when the element did become focused.
}
}
If I change the cursor and come back to the flash button, it should say change the flash status again.
无论您选择闪光按钮多少次,在说更改其值之前,始终应先读出所需的状态。
下面的代码中提供了一个示例:
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 { }
}
}
How can I achieve this?
要么尝试使用 UIAccessibilityFocus
非正式协议(protocol)来捕捉焦点变化,要么只使用上面的代码片段来更改可访问性元素的状态:您可以结合两者,您可以根据自己的编码环境调整这些概念。 ;o)
还不够看this site致力于 a11y 开发人员,其中提供了代码片段和插图,可以为您的实现找到另一种解决方案。
关于ios - 如何捕捉可访问性焦点的变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56528450/