iOS开发实录iOS精品文章-实用技巧&博客总结iOS实践

支持voice over

2016-02-28  本文已影响463人  RY_zheng

开启VoiceOver:

iPhone 设置-->通用 -->辅助功能 -->VoiceOver
开启VoiceOver后交户意义会发生变化:

VoiceOver功能

voice over辅助功能和特点

模拟器调试VoiceOver

单击:a single-click focuses the inspector on an element

双击:simulate a tap on an element

滚动:先把"Accessibility inspector"关闭,用鼠标滚动或者双手触控键盘滑动

给自定义UI视图设置voice over

两个方法:
1.设置isAccessibilityElement

self.isAccessibilityElement = YES;
self.accessibilityLabel = @"ovice over 提示内容";

/* 设置属性 */

2.遵守UIAccessibility协议,并实现其中isAccessibilityElement方法

 /* 设置属性的方法 */
 
- (BOOL)isAccessibilityElement
{
   return YES;
}

设置子View voice over

如果视图还含有子视图,并且是这些子视图与用户交互。比如UITableView视图,我们需要设置子视图UITableViewCell的voice over属性,同时保证UITableView不支持voice over。

两个方法:

1.只给子视图设置isAccessibilityElement。比如UITableView视图不设置isAccessibilityElement,直接设置UITableViewCell的isAccessibilityElement,和accessibilityLabel。

2.自定义的父视图(子视图的容器)实现 UIAccessibilityContainer 协议. 这个协议定义了一个数组accessibleElements存储着所有voice over 可访问的元素。

@implementation MultiFacetedView
- (NSArray *)accessibleElements
{
   if ( _accessibleElements != nil )
   {
      return _accessibleElements;
   }
   _accessibleElements = [[NSMutableArray alloc] init];
 
   /* Create an accessibility element to represent the first contained element and initialize it as a component of MultiFacetedView. */
   UIAccessibilityElement *element1 = [[[UIAccessibilityElement alloc] initWithAccessibilityContainer:self] autorelease];
 
   /* Set attributes of the first contained element here. */
   [_accessibleElements addObject:element1];
 
   /* Perform similar steps for the second contained element. */
   UIAccessibilityElement *element2 = [[[UIAccessibilityElement alloc] initWithAccessibilityContainer:self] autorelease];
 
   /* Set attributes of the second contained element here. */
   [_accessibleElements addObject:element2];
 
   return _accessibleElements;
}
 
/* The container itself is not accessible, so MultiFacetedView should return NO in isAccessiblityElement. */
- (BOOL)isAccessibilityElement
{
   return NO;
}
 
/* The following methods are implementations of UIAccessibilityContainer protocol methods. */
- (NSInteger)accessibilityElementCount
{
   return [[self accessibleElements] count];
}
 
- (id)accessibilityElementAtIndex:(NSInteger)index
{
   return [[self accessibleElements] objectAtIndex:index];
}
 
- (NSInteger)indexOfAccessibilityElement:(id)element
{
   return [[self accessibleElements] indexOfObject:element];
}
@end

自定义属性信息

两个方法:

直接设置性

@implementation MyCustomViewController
- (id)init
{
  _view = [[MyCustomView alloc] initWithFrame:CGRectZero];
 
  [_view setIsAccessibilityElement:YES];
  [_view setAccessibilityTraits:UIAccessibilityTraitButton];
  [_view setAccessibilityLabel:NSLocalizedString(@"提示的内容", nil)];  
  [_view setAccessibilityHint:NSLocalizedString(@"提示", nil)];
}

实现UIAccessibility的方法

@implementation MyCustomView
- (BOOL)isAccessibilityElement
{
   return YES;
}
 
- (NSString *)accessibilityLabel
{
   return NSLocalizedString(@"MyCustomView.label", nil);
}
 
/* This custom view behaves like a button. */
- (UIAccessibilityTraits)accessibilityTraits
{
   return UIAccessibilityTraitButton;
}
 
- (NSString *)accessibilityHint
{
   return NSLocalizedString(@"MyCustomView.hint", nil);
}
@end

UITableView支持Voice Over

如果你的UItableViewCell的里面还有很多子 view,并且你想点击它们的时候有反应。你需要:

在UITabaleViewCell类本就设计遵守UIAccessibilityContainer,所以我们可以直接写

@implementation CurrentWeather
/* This is a view that provides weather information. It contains a city subview and a temperature subview, each of which provides a separate label. */
- (NSString *)accessibilityLabel
{
    NSString *weatherCityLabel = [self.weatherCity accessibilityLabel];
    NSString *weatherTempLabel = [self.weatherTemp accessibilityLabel];
 
    /* Combine the city and temperature information so that VoiceOver users can get the weather information with one gesture. */
    return [NSString stringWithFormat:@"%@, %@", weatherCityLabel, weatherTempLabel];
}
@end

或者,设置

self.accessibilityLabel = @"ovice over 提示内容";

推荐文章:

accessibility programing guide

voice over: UIAccessibility

上一篇下一篇

猜你喜欢

热点阅读