refactored for accessible elements
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
c01a700b79
commit
01923afe4a
@ -13,79 +13,62 @@ public protocol CustomRotorable: UIViewController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
extension CustomRotorable {
|
extension CustomRotorable {
|
||||||
|
|
||||||
/// Adds CustomRotor to the accessibilityCustomRotors array.
|
|
||||||
/// - Parameters:
|
|
||||||
/// - name: Name that will show up in the Rotor picker.
|
|
||||||
/// - trait: Any UIView that has this traits will be added to this Name.
|
|
||||||
internal func addCustomRotor(with name: String, for trait: UIAccessibilityTraits) {
|
internal func addCustomRotor(with name: String, for trait: UIAccessibilityTraits) {
|
||||||
//filter out old rotors with same name
|
|
||||||
accessibilityCustomRotors = (accessibilityCustomRotors ?? []).filter { $0.name != name }
|
accessibilityCustomRotors = (accessibilityCustomRotors ?? []).filter { $0.name != name }
|
||||||
|
|
||||||
//create new rotor
|
|
||||||
let newRotor = UIAccessibilityCustomRotor(name: name) { [weak self] predicate in
|
let newRotor = UIAccessibilityCustomRotor(name: name) { [weak self] predicate in
|
||||||
guard let self else { return nil }
|
guard let self = self else { return nil }
|
||||||
|
|
||||||
let views = self.view.accessibleElements(with: trait)
|
let elements = self.view.accessibleElements(with: trait)
|
||||||
|
|
||||||
guard !views.isEmpty else { return nil }
|
guard !elements.isEmpty else { return nil }
|
||||||
|
|
||||||
let currentIndex = views.firstIndex(where: { $0 === predicate.currentItem.targetElement })
|
let currentIndex = elements.firstIndex(where: { ($0 as AnyObject) === predicate.currentItem.targetElement })
|
||||||
let count = views.count
|
let count = elements.count
|
||||||
|
|
||||||
//find the nextIndex
|
|
||||||
let nextIndex: Int
|
let nextIndex: Int
|
||||||
switch predicate.searchDirection {
|
switch predicate.searchDirection {
|
||||||
case .next:
|
case .next:
|
||||||
if let currentIndex, currentIndex != count - 1{
|
nextIndex = currentIndex.map { ($0 + 1) % count } ?? 0
|
||||||
//go forwards
|
|
||||||
nextIndex = currentIndex + 1
|
|
||||||
} else {
|
|
||||||
//get the first
|
|
||||||
nextIndex = 0
|
|
||||||
}
|
|
||||||
case .previous:
|
case .previous:
|
||||||
if let currentIndex, currentIndex != 0 {
|
nextIndex = currentIndex.map { ($0 - 1 + count) % count } ?? 0
|
||||||
//go backwards
|
|
||||||
nextIndex = currentIndex - 1
|
|
||||||
} else {
|
|
||||||
//get the last
|
|
||||||
nextIndex = count - 1
|
|
||||||
}
|
|
||||||
@unknown default:
|
@unknown default:
|
||||||
//get the first
|
|
||||||
nextIndex = 0
|
nextIndex = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
return UIAccessibilityCustomRotorItemResult(targetElement: views[nextIndex], targetRange: nil)
|
guard let element = elements[nextIndex] as? NSObjectProtocol else { return nil }
|
||||||
|
return UIAccessibilityCustomRotorItemResult(targetElement: element, targetRange: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
//append rotor
|
|
||||||
accessibilityCustomRotors?.append(newRotor)
|
accessibilityCustomRotors?.append(newRotor)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Loads all of the custom rotors for the screen.
|
|
||||||
public func loadCustomRotors() {
|
public func loadCustomRotors() {
|
||||||
customRotors.forEach { addCustomRotor(with: $0.name, for: $0.trait) }
|
customRotors.forEach { addCustomRotor(with: $0.name, for: $0.trait) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public extension UIView {
|
public extension UIView {
|
||||||
|
|
||||||
/// Gets all of the Views that has the matching accessibilityTrait.
|
func accessibleElements(with trait: UIAccessibilityTraits) -> [Any] {
|
||||||
/// - Parameter trait: This is the trailt for the accessibilityTrait property of a view.
|
var elements: [Any] = []
|
||||||
/// - Returns: An array of RotorItemResult
|
|
||||||
func accessibleElements(with trait: UIAccessibilityTraits) -> [UIView] {
|
|
||||||
var elements: [UIView] = []
|
|
||||||
|
|
||||||
//add your self if you meet the requirements
|
|
||||||
if isAccessibilityElement, accessibilityTraits.contains(trait) {
|
if isAccessibilityElement, accessibilityTraits.contains(trait) {
|
||||||
elements.append(self)
|
elements.append(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
//loop through your subviews
|
if let customAccessibilityElements = accessibilityElements {
|
||||||
subviews.forEach { elements.append(contentsOf: $0.accessibleElements(with: trait)) }
|
elements.append(contentsOf: customAccessibilityElements.filter { element in
|
||||||
|
if let element = element as? UIAccessibilityElement {
|
||||||
|
return element.accessibilityTraits.contains(trait)
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
subviews.forEach { elements.append(contentsOf: $0.accessibleElements(with: trait)) }
|
||||||
|
}
|
||||||
|
|
||||||
return elements
|
return elements
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user