iOS的可访问性 – button集中

在我的应用程序中有3个button,当打开语音input并且用户向右滑动时,下一个button被选中。

– button可访问性焦点与按下的button不同。

提问的理由 – 我希望应用程序在selectbutton1时公布“AAA”,按下button1时公布“BBB”。

问题

  • 如何实现上述目标?
  • 有没有办法在selectbutton时调用方法。

视图(在这种情况下button)需要实现协议UIAccessibilityFocus

 - (void)accessibilityElementDidBecomeFocused - (void)accessibilityElementDidLoseFocus - (BOOL)accessibilityElementIsFocused 

这里有一些ObjectiveC代码行来执行你想要做的事情:

 // Create button UIButton *b = [[UIButton alloc] init]; // Set it's title [b setTitle:@"AAA" forState:UIControlStateNormal]; // Set the label, ie 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 } } } 

看看苹果无障碍指南了解更多信息。