0

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.

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()
}

Any ideas why? Full project available at https://github.com/AaronBratcher/SliderTest

Aaron Bratcher
  • 6,051
  • 2
  • 39
  • 70

1 Answers1

0

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
}
Aaron Bratcher
  • 6,051
  • 2
  • 39
  • 70