如何创建左/右音频平衡滑块并让画外音读出自定义值?

时间:2018-02-21 04:28:08

标签: ios swift uislider voiceover

我在Interface Builder中创建了一个UISlider,并希望用它来调整两个通道之间的L / R音量平衡。这就是现在的样子:  enter image description here 我目前的代码如下:

import UIKit

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

override func awakeFromNib() {
    super.awakeFromNib()

    self.slider.minimumTrackTintColor = .gray
    self.slider.maximumTrackTintColor = .gray
}

@IBOutlet var slider: UISlider!
override func updateUI() {
    self.slider.value = self.audioInterface?.balance ?? 0.0
    self.slider.isEnabled = !(self.audioInterface?.muted ?? true)
}


@IBAction func sliderChanged(_ slider: UISlider) {
    self.audioInterface?.balance = slider.value
}

}

在Interface Builder中,我将滑块值设置为0,将最小值设置为-1,将最大值设置为1,并设置“启用用户交互”,“可调整”,以及'允许直接互动'特征。但是当我打开画外音时,我会以10%的增量报告值,从“50%”开始。

我希望滑块的行为与通用>中的此视图中的行为相同辅助功能设置屏幕:

enter image description here

在这个位置,画外音上写着:“剩下30%,右边70%”

我如何完成我之后的目标?

感谢!!!

1 个答案:

答案 0 :(得分:2)

您可以设置VoiceOver与视图属性(通过NSObject)说话的值:

  • .accessibilityLabel
  • .accessibilityHint
  • .accessibilityValue

当滑块移动时,VoiceOver会说出滑块.accessibilityValue

我将滑块设置为min of 0max of 1,以便该值为百分比,例如0.35相当于35%。然后我四舍五入到最接近的10%,这样当滑块移动时,VoiceOver会说70% left 30% right之类的值。

import UIKit

class ViewController: UIViewController {

    @IBOutlet var slider: UISlider!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        setSliderAccessibilityValue()           
    }

    @IBAction func sliderMoved(sender: Any?) {
        setSliderAccessibilityValue()    
    }

    func setSliderAccessibilityValue() {

        let right_percent = Int(
            round(slider.value * 10)  //=> round(0.2632 * 10) => round(2.632) => 3
        ) * 10  //=> 30
                                                //       30% = right %
        let left_percent = 100 - right_percent  //        |
                                                // left|..V......|right
                                                // balance: 70% left, 30% right
        let accValue = "\(left_percent)% left \(right_percent)% right"           
        slider.accessibilityValue = accValue

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}