我有一个子类 UIAccessibilityElement 用于在我的 Cocos2D 应用程序中启用可访问性。
在运行时,此类的实例都可以通过 iOS 的辅助功能进行导航,这很棒,但我特别需要能够更改 iOS 使用的语言和口音(这仅适用于 iOS7+)。元素。
我注意到要做到这一点,我应该覆盖:
- (NSString*) accessibilityElementLanguage;
消息,我已经完成了。
我已经通过模拟器中的可访问性检查器确认正在返回正确的语言 ID(它显示在检查器中)。
然而,当画外音读取与元素关联的文本时,它坚持使用默认设备语音和语言,因此西班牙语单词听起来完全不正确是可以理解的。
我不确定我做错了什么。当语言设置失败时,我尝试了一种解决方法,我将处理accessibilityElementDidBecomeFocused 回调并使用我自己的代码读取文本,但现在我什至没有获得焦点事件的回调,除非我正在运行模拟器启用了可访问性检查器,我触摸了我的元素之一。
我确定我这里有一些简单的错误。任何帮助,将不胜感激。
类接口的代码是:
#import <UIKit/UIKit.h>
#import "CCSwitchableNode.h"
#import <UIKit/UIAccessibility.h>
@interface AccessibleSwitchableNode : UIAccessibilityElement <UIAccessibilityIdentification>
- (id) initWithNode:(id<CCSwitchableNode>)node inContainer:(id)container;
+ (AccessibleSwitchableNode*) accessibleSwitchableWithNode:(id<CCSwitchableNode>)node inContainer:(id)container;
@property (nonatomic, retain) id<CCSwitchableNode> node;
@end
和实施:
#import "AccessibleSwitchableNode.h"
#import <UIKit/UIAccessibility.h>
#import <UIKit/UIAccessibilityElement.h>
@implementation AccessibleSwitchableNode {
BOOL isFocused_Local;
}
- (id) initWithNode:(id<CCSwitchableNode>)node inContainer:(id)container {
self = [super initWithAccessibilityContainer:container];
if (self != nil) {
isFocused_Local = NO;
self.node = node;
// This code is a quick hack to translate the position from Cocos2d in one specific orientation
// to UIKit. To be finessed.
//
CGPoint uiPoint = [[CCDirector sharedDirector] convertToUI:[node switchableNodePosition]];
uiPoint.x = [CocosUtil screenWidth] - uiPoint.x;
self.accessibilityFrame =
CGRectMake(uiPoint.y-([node switchableNodeSize].height/2.0f),
uiPoint.x-([node switchableNodeSize].width/2.0f),
[node switchableNodeSize].height,
[node switchableNodeSize].width);
self.accessibilityLabel = [node textForSpeaking];
self.accessibilityValue = [node textForSpeaking];
self.accessibilityTraits = UIAccessibilityTraitButton;
self.accessibilityActivationPoint = CGPointMake(uiPoint.y, uiPoint.x);
}
return self;
}
+ (AccessibleSwitchableNode*) accessibleSwitchableWithNode:(id<CCSwitchableNode>)node inContainer:(id)container {
return [[[AccessibleSwitchableNode alloc] initWithNode:node inContainer:container] autorelease];
}
- (void) dealloc {
self.node = nil;
[super dealloc];
}
// Called when the object is first added, but has no effect on pronunciation.
//
- (NSString*) accessibilityLanguage {
return [self.node languageForText];
}
// Never called unless on the simulator with the inspector active.
//
- (void) accessibilityElementDidBecomeFocused {
NSLog(@"accessibilityElementDidBecomeFocusedd: %@", self.accessibilityLabel);
isFocused_Local = YES;
}
// Never called unless on the simulator with the inspector active.
//
- (void) accessibilityElementDidLoseFocus {
NSLog(@"accessibilityElementDidLoseFocus: %@", self.accessibilityLabel);
isFocused_Local = NO;
}
// Never called unless on the simulator with the inspector active.
//
- (BOOL) accessibilityElementIsFocused {
NSLog(@"accessibilityElementIsFocused: %@", self.accessibilityLabel);
return isFocused_Local;
}
// Never called unless on the simulator with the inspector active.
//
- (BOOL) accessibilityActivate {
if ([self.node isSwitchSelectable] == YES) {
[self.node switchSelect];
return YES;
} else {
return NO;
}
}
- (BOOL) isAccessibilityElement {
return [self.node isSwitchSelectable];
}
@end
我添加到 AccessibilityElements 的 Cocos2D 节点的协议是:
#import <Foundation/Foundation.h>
@protocol CCSwitchableNode <NSObject>
// Should return the size of the node on screen.
//
- (CGSize) switchableNodeSize;
// Should return the position (center) of the node in screen coordinates.
//
- (CGPoint) switchableNodePosition;
// Should return YES if the node is currently able to accept taps by the user.
//
- (BOOL) isSwitchSelectable;
// Should cause the node, be it a button of some sort, or something else, to act as if it has been
// tapped. This will be called when a switch is used to "select" an item on the screen.
//
- (void) switchSelect;
// Should return YES if the node would prefer to highlight itself to the user as selectable. If this
// happens, the SwitchControlLayer will call the setSwitchHighlighted: as appropriate in
// order to turn on or off the highlight.
//
- (BOOL) hasOwnHighlight;
// Should return a NSString containing the text to be spoken when the node is highlighted. If no text is
// to be spoken, then nil should be returned.
//
- (NSString*) textForSpeaking;
// Should return a NSString containing the language locale to use when speaking the nodes text.
//
- (NSString*) languageForText;
@optional
// This optional method should be implemented in classes that can return YES from the hasOwnHighlight
// method. The SwitchControlLayer will call this instead of using it's own built-in highlight
// to turn on, or off the highlight.
//
- (void) setSwitchHighlighted:(BOOL)highlighted;
@end