-->

UIAccessibility change UITableView voiceover annou

2019-04-08 07:06发布

问题:

With VoiceOver enabled a user can use a 3 finger swipe gesture to scroll TableViews. VoiceOver verbally announces to the user a phrase indicating their location on the tableview i.e. the rows that are visible such as "Rows 1 to 4 of 5".

I would like to override this verbal prompt and get voiceover to announce something else to the user.

回答1:

You can't change only the message. You will have to override accessibilityScroll: and do the scrolling and then post the scroll announcement (at least I don't know any other way). It isn't super hard though.

What to do

Since you have a table view you can scroll to a certain row using scrollToRowAtIndexPath:atScrollPosition:animated:. You could decide that one accessibility element is X number of rows. In that case you get the set of visible rows from the table view and add or remove X to the row of the last or first cell (depending on if the scroll is up or down).

Finally to announce the that the table view did scroll you should post a "page scrolled" notification and pass the text to be read. Finally you should return YES to say that you handled the scrolling (if you don't then the event will continue to propagate).

Example implementation

A basic implementation could look something like this (I'm making some assumptions that may change depending on your code, like that there is only one section):

- (BOOL)accessibilityScroll:(UIAccessibilityScrollDirection)direction 
{
    BOOL isScrollingUp = NO;
    switch (direction) {
        case UIAccessibilityScrollDirectionUp: {
            isScrollingUp = YES;
        } break;

        case UIAccessibilityScrollDirectionDown: {
            isScrollingUp = NO;
        } break;

        default:
            // These cases are not handled
            return NO;
    }

    NSInteger numberOfCellsToScroll = 5; // Any number you'd like

    NSInteger newRow = -1;
    if (isScrollingUp) {
        newRow = [self.tableView indexPathsForVisibleRows][0].row - numberOfCellsToScroll;
    } else {
        newRow = [[self.tableView indexPathsForVisibleRows] lastObject].row + numberOfCellsToScroll;
    }

    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:newRow inSection:0] 
                          atScrollPosition:UITableViewScrollPositionMiddle 
                                  animated:YES];

    UIAccessibilityPostNotification(UIAccessibilityPageScrolledNotification,
                                    @"YOUR CUSTOM TEXT FOR THE SCROLL HERE");

    return YES; // We handled the scroll
}


回答2:

Use UIScrollViewAccessibilityDelegate and implement the method -accessibilityScrollStatusForScrollView:

Example

- (NSString *)accessibilityScrollStatusForScrollView:(UIScrollView *)scrollView
{
    return @"Your text";
}