accessibility增量/减量未调用 [英] accessibilityIncrement / Decrement not called
问题描述
我看了看,找不到适合我的答案。我将UIControl子类化为创建双旋钮滑块控件。我希望每个旋钮都可用于画外音。
I looked and cannot find an answer that works for me. I have subclassed UIControl to create a double-knob slider control. I want each knob to be available for voiceover.
为此,我创建UIAccessibilityElements并将其添加到数组中:
To do this, I create UIAccessibilityElements and add them to an array:
func addAccessibilityElements() {
axKnobs = []
let lowKnob = UIAccessibilityElement(accessibilityContainer: self)
lowKnob.accessibilityLabel = doubleKnob ? lowValueKnobAccessibilityLabel : valueKnobAccessibilityLabel
lowKnob.accessibilityPath = UIAccessibilityConvertPathToScreenCoordinates(knobBezierPath(lowKnobPoint), self)
lowKnob.accessibilityTraits = UIAccessibilityTraitAdjustable
lowKnob.accessibilityValue = "\(lowValue)"
axKnobs.append(lowKnob)
if doubleKnob, let highKnobPoint = highKnobPoint {
let highKnob = UIAccessibilityElement(accessibilityContainer: self)
highKnob.accessibilityLabel = highValueKnobAccessibilityLabel
highKnob.accessibilityPath = UIAccessibilityConvertPathToScreenCoordinates(knobBezierPath(highKnobPoint), self)
highKnob.accessibilityTraits = UIAccessibilityTraitAdjustable
highKnob.accessibilityValue = "\(highValue)"
axKnobs.append(highKnob)
}
}
这似乎很不错。调用了这些方法,并且界面似乎正常工作。
This seems to work perfect. These methods are called and the interface seems to work right:
override func accessibilityElementCount() -> Int {
return axKnobs.count
}
override func indexOfAccessibilityElement(element: AnyObject) -> Int {
let index = axKnobs.indexOf(element as! UIAccessibilityElement)!
if index == 0 {
currentKnob = .Low
} else {
currentKnob = .High
}
return index
}
override func accessibilityElementAtIndex(index: Int) -> AnyObject? {
return axKnobs[index]
}
但是,我的最后两种方法
However, my last 2 methods (accessibilityIncrement and accessibilityDecrement) in the class extension aren't being called at all.
override func accessibilityIncrement() {
if currentKnob == .None {
return
}
if currentKnob == .High {
highValue = max(highValue + 10, maximumValue)
} else {
if doubleKnob {
lowValue = max(lowValue + 10, highValue - 1)
} else {
lowValue = max(lowValue + 10, maximumValue)
}
}
updateDelegate()
redraw()
}
override func accessibilityDecrement() {
if currentKnob == .None {
return
}
if currentKnob == .High {
highValue = min(highValue - 10, lowValue + 1)
} else {
lowValue = min(lowValue - 10, minimumValue)
}
updateDelegate()
redraw()
}
任何想法为何?完整项目可在 https://github.com/AaronBratcher/SliderTest
Any ideas why? Full project available at https://github.com/AaronBratcher/SliderTest
推荐答案
UIAccessibilityElements调用了这2种方法,而不是UIControl子类。
UIAccessibilityElements have those 2 methods called, not the UIControl subclass.
extension DLSlider {
class KnobAccessibilityElement: UIAccessibilityElement {
var onIncrement: ((knob: UIAccessibilityElement) -> Void)?
var onDecrement: ((knob: UIAccessibilityElement) -> Void)?
override func accessibilityIncrement() {
if let callback = onIncrement {
callback(knob: self)
}
}
override func accessibilityDecrement() {
if let callback = onDecrement {
callback(knob: self)
}
}
}
func addAccessibilityElements() {
axKnobs = []
let lowKnob = KnobAccessibilityElement(accessibilityContainer: self)
lowKnob.accessibilityLabel = doubleKnob ? lowValueKnobAccessibilityLabel : valueKnobAccessibilityLabel
lowKnob.accessibilityPath = UIAccessibilityConvertPathToScreenCoordinates(knobBezierPath(lowKnobPoint), self)
lowKnob.accessibilityTraits = UIAccessibilityTraitAdjustable
lowKnob.accessibilityValue = "\(lowValue)"
lowKnob.onIncrement = { [unowned self] (knob: UIAccessibilityElement) in
self.incrementKnob(knob)
}
lowKnob.onDecrement = { [unowned self] (knob: UIAccessibilityElement) in
self.decrementKnob(knob)
}
axKnobs.append(lowKnob)
if doubleKnob, let highKnobPoint = highKnobPoint {
let highKnob = KnobAccessibilityElement(accessibilityContainer: self)
highKnob.accessibilityLabel = highValueKnobAccessibilityLabel
highKnob.accessibilityPath = UIAccessibilityConvertPathToScreenCoordinates(knobBezierPath(highKnobPoint), self)
highKnob.accessibilityTraits = UIAccessibilityTraitAdjustable
highKnob.accessibilityValue = "\(highValue)"
highKnob.onIncrement = { [unowned self] (knob: UIAccessibilityElement)in
self.incrementKnob(knob)
}
highKnob.onDecrement = { [unowned self] (knob: UIAccessibilityElement) in
self.decrementKnob(knob)
}
axKnobs.append(highKnob)
}
}
override func accessibilityElementCount() -> Int {
return axKnobs.count
}
override func indexOfAccessibilityElement(element: AnyObject) -> Int {
return axKnobs.indexOf(element as! UIAccessibilityElement)!
}
override func accessibilityElementAtIndex(index: Int) -> AnyObject? {
return axKnobs[index]
}
... // other methods here
}
这篇关于accessibility增量/减量未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!