问题
There are 3 buttons in my app, when voice over is turned on and the user swipes to the right, the next button is selected.
Note - The button accessibility focus is different from button pressed.
Reason for asking - I want the app to announce "AAA" when the button 1 is selected and "BBB" when button 1 is pressed.
Questions
- How can the above be achieved ?
- Is there a way to call a method when a button is selected.
回答1:
The View (in this case button) needs to implement the protocol UIAccessibilityFocus
- (void)accessibilityElementDidBecomeFocused
- (void)accessibilityElementDidLoseFocus
- (BOOL)accessibilityElementIsFocused
回答2:
Here is a few ObjectiveC lines of code to perform what you want to do :
// Create button
UIButton *b = [[UIButton alloc] init];
// Set it's title
[b setTitle:@"AAA" forState:UIControlStateNormal];
// Set the label, i.e. it's name
[b setAccessibilityLabel:NSLocalizedString(@"AAA", @"")];
// Set button action description
[b setAccessibilityHint:NSLocalizedString(@"AAA button action description", @"")];
// Ad the button target, it's action
[b addTarget:self action:@selector(yourMethod:) forControlEvents:UIControlEventTouchUpInside];
// Add an observer
[b addObserver:self forKeyPath:@"state" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
// Implement this method in your controller
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
if (object == self.b && [keyPath isEqualToString:@"state"]) {
if (self.b.state == UIControlStateSelected) {
// DO WHAT YOU WANT TO DO ON SELECTION
}
}
}
Take a look at Apple accessibility Guide for more infos.
来源:https://stackoverflow.com/questions/27167471/ios-accessibility-button-focussed