Reputation: 45
I'd like to call a method every time a different element is focused while VoiceOver is active. I was hoping there would be some UIAccessibilityNotification
for this, but I can't seem to find any.
Ultimately, my goal is to add an additional condition prior to reading the accessibility label. For example, as opposed to saying (by default) "If UIButton
becomes focused: read label", I'd like to be able to say "When UIButton
becomes focused AND UIButton
's background color is blue: read label".
So my question is: how do I either add an additional condition prior to reading the label, or receive a notification when a new element becomes focused?
Upvotes: 4
Views: 3671
Reputation: 20609
You can use the UIAccessibilityFocus
protocol to detect changes in focus by accessibility clients (including VoiceOver). Note that UIAccessibilityFocus
is an informal protocol that each accessibility element must implement independently.
That said, for your use case, Aaron is right to suggest returning a different accessibilityLabel
under each condition.
Upvotes: 1
Reputation: 66302
You can't explicitly tell when the user moves the VoiceOver cursor (just like you can't tell where a sighted user is looking).
For the behavior you want, you have two options:
accessibilityLabel
to an appropriate value whenever the other conditions change.Subclass UIButton
and override its accessibilityLabel
getter method:
- (NSString *) accessibilityLabel {
if (SOME_CONDITION) {
return @"Hooray!";
} else {
return @"Womp womp";
}
}
If you need to disable an item entirely, rather than returning nil
or a blank string, you should set its accessibilityElementsHidden
property to YES
.
Upvotes: 2