如何捕捉可访问性焦点的变化

如何捕捉可访问性焦点的变化

本文介绍了如何捕捉可访问性焦点的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想捕捉可访问性焦点的变化.

我搜索并尝试了 accessibilityElementDidBecomeFocused 但在光标改变后没有触发.我想为按钮添加值,并在光标更改后删除此值.

我的代码是这样的:

override func viewDidLoad() {flashButton.accessibilityLabel = "更改闪光灯状态"flashButton.accessibilityTraits = [.button]flashButton.accessibilityIdentifier = "flashButton"}@IBAction func flashButtonTapped(_ sender: Any) {var currentFlashStatus = "关闭"守卫让设备 = AVCaptureDevice.default(AVCaptureDevice.DeviceType.builtInWideAngleCamera, for: AVMediaType.video, position: .back) else { return }保护 device.hasTorch else { return }做 {试试 device.lockForConfiguration()如果 device.torchMode == .on {device.torchMode = .offflashButton.image = UIImage(named: "flashOffIcon")currentFlashStatus = "关闭"} 别的 {做 {试试 device.setTorchModeOn(level: 1.0)flashButton.image = UIImage(named: "flashOnIcon")currentFlashStatus = "on"} 抓住 {打印(错误)}}device.unlockForConfiguration()} 抓住 {打印(错误)}flashButton.accessibilityLabel = "打开 \(currentFlashStatus) 闪光灯"}

首先,它显示更改闪光灯状态",如果我双击它,它会显示打开闪光灯状态",这是正确的.

如果我改变光标并回到闪光按钮,它应该说再次改变闪光状态.但它说打开闪光灯状态"

我怎样才能做到这一点?

解决方案

如果您想使用 UIAccessibilityFocus 非正式协议方法,您必须直接在您的对象中而不是在视图控制器中覆盖它们(请参阅 这个答案).创建一个 UIButton 的子类,例如:

导入 UIKit类 FlashButton:UIButton {}扩展 FlashButton {覆盖 open func accessibilityElementDidBecomeFocused() {//当元素成为焦点时要执行的操作.}}

如果我改变光标并回到闪光按钮,它应该说再次改变闪光状态.

无论您选择闪光按钮多少次,在说要更改其值之前,应始终先读出所需的状态.

以下代码中提供了一个示例:

class FlashButton: UIButton {私有变量 intStatus: Bool = false私有变量 a11yLabelStatus: String = "off"变量状态:布尔{得到 { 返回 intStatus }设置(新值){ intStatus = newValue }}}扩展 FlashButton {覆盖 open func accessibilityElementDidBecomeFocused() {//当元素成为焦点时要执行的操作.}覆盖 open func accessibilityElementDidLoseFocus() {//当元素失去焦点时要执行的操作.}//这个原生的a11y函数代替你定义的IBAction来改变和读出flash状态.覆盖 func accessibilityActivate() ->布尔{intStatus = (intStatus == true) ?假:真a11yLabelStatus = (intStatus == true) ?开关"UIAccessibility.post(通知:.公告,参数:闪存状态为" + a11yLabelStatus)返回真}覆盖 var accessibilityLabel: String?{get { return "闪存状态为 " + a11yLabelStatus }放 { }}覆盖 var 辅助功能提示:字符串?{get { return "双击以更改值."}放 { }}}

我怎样才能做到这一点?

要么尝试使用 UIAccessibilityFocus 非正式协议来捕获焦点更改,要么仅使用上面的代码片段来更改辅助功能元素的状态:您可以将两者结合起来,您就可以在您的编码环境中调整这些概念.;o)

如果还不够,看看本网站 专为所有开发人员而设,其中提供了代码片段和插图,可以为您的实施找到另一种解决方案.

I want to catch the accessibility focus change.

I searched and tried accessibilityElementDidBecomeFocused but didn't trigger after cursor changed. I want to add value to button and after cursor change remove this value.

My code is like:

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"
}

First, it reads "change the flash status" and if i double tap it reads "Turned on the flash status" which is correct.

If i change the cursor and come back to the flash button, it should say change the flash status again. But it says "turned on the flash status"

How can i achieve this?

解决方案

If you want to use the UIAccessibilityFocus informal protocol methods, you MUST override them in your object directly and not in the view controller (see this answer). Create a subclass of UIButton for instance:

import UIKit

class FlashButton: UIButton {}

extension FlashButton {

    override open func accessibilityElementDidBecomeFocused() {
    // Actions to be done when the element did become focused.
    }
}

Whatever the number of times you select the flash button, the desired status should be always read out first before saying to change its value.

An example is provided in the code hereafter:

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 { }
    }
}

Either you try the UIAccessibilityFocus informal protocol to catch the focus change or you just use the code snippet above in order to change the status of your accessibility element: you can combine both, it's up to you to adapt these concepts in your coding environment. ;o)

If it isn't enough, take a look at this site dedicated to a11y developers where code snippets and illustrations are available to find out another solution for your implementation.

这篇关于如何捕捉可访问性焦点的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 16:59