如何以编程方式更改UIButton的配音值?

时间:2018-02-20 21:52:34

标签: ios swift voiceover

我在InterfaceBuilder中创建了一个UISwitch来打开和关闭音频。该开关工作正常,但我想将画外音从当前的'0'和'1'读出的值更改为'Off'和'On'。我的代码如下:

import UIKit

class AudioStreamTableViewCell: UITableViewCell {
    weak var controller: EventDetailsViewController!
    weak var audioInterface: EventAudioInterface? { didSet { if self.audioInterface !== oldValue { self.updateUI() }}}
    func updateUI() {}
    override func awakeFromNib() {
        super.awakeFromNib()
        self.updateUI()
    }

}

class MuteStreamTableViewCell: AudioStreamTableViewCell {
    static let identifier = "MuteStreamTableViewCell"

    @IBOutlet var muteSwitch: UISwitch!

    override func updateUI() {
        self.muteSwitch.isOn = self.audioInterface?.muted ?? false
    }

    @IBAction func switchChanged(_ muteSwitch: UISwitch) {
        self.controller.setMuted(muteSwitch.isOn, on: self.audioInterface)

        if muteSwitch.isOn {
            self.muteSwitch.accessibilityValue = "on"
        } else {
            self.muteSwitch.accessibilityValue = "off"
        }
    }

}

画外音仍然会说“0”和“1”。我做错了什么?

谢谢!

1 个答案:

答案 0 :(得分:2)

雨燕4.2

我不得不继承UISwitch并重写accessibilityValue

class AccessibilityUiSwitch: UISwitch {
    override var accessibilityValue: String? {
        get {
            return isOn ? "on" : "off"
        }
        set {
            self.accessibilityValue = newValue
        }
    }
}