I need to know which element is currently become focused by accessibility focus engine. In a simple case, let' say I have come cell with a couple of label. I need to know (i.e. print on console, or doing other stuff) which object is currently focused. Them same if I had a view with a lot of subviews, of different type. I think I should use elementFocusedNotification, and as told in docs, use the key UIAccessibilityElementFocusedKeyElement
. I think I should pass the object in the method, but how?.
among others, I looked here, here and here for info, but cannot find solutions to know which element is currently focused.
in my cellForRowAt indexPath:
in the cell:
override func accessibilityElementDidBecomeFocused() {
NotificationCenter.default.post(
name: NSNotification.Name(rawValue: UIAccessibility.elementFocusedNotification.rawValue),
object: self.subviews.first(where: {$0.isFocused}),
userInfo: ["UIAccessibilityElementFocusedKeyElement": "hello there"]
)
}
in the controller's viewDidLoad:
NotificationCenter.default.addObserver(
self,
selector: #selector(self.doSomething(_:)),
name: NSNotification.Name(rawValue: UIAccessibility.elementFocusedNotification.rawValue),
object: nil
)
in the same controller
@objc func catchNotification(_ notification: Notification) {
//print("subscribed, notification: \(notification)")
if let myNotification1 = notification.userInfo?["UIAccessibilityElementFocusedKeyElement"] {
print("+++", myNotification1)
}
}