我有一个类,它的子类是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
答案 0 :(得分:1)
这可能会让你感到惊讶和恐惧,但即便是苹果公司也会出现问题。如果您遇到意外或主观上的错误&#34;通过全面的复制步骤和示例项目,考虑 filing a Radar 。
答案 1 :(得分:1)
根据我上面的一些评论,简短的回答是iOS7不像VoiceOver那样支持Switch Control的各种回调和API。适用于VoiceOver的功能不一定适用于Switch Control。
向Apple提交了一个错误,并在那里与他们进行了互动,针对iOS8测试我的代码表明,对于Switch Control的支持已经改进了beta 5,但仍然不完整。 accessibilityElementDidBecomeFocused回调现在似乎可以正常工作,但是Switch Control仍然拒绝使用accessibilityElementLanguage回调。