0

我有一个子类 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
4

2 回答 2

1

这可能会让您感到惊讶和恐惧,但即使是 Apple 也会出现错误。如果您遇到意外或主观上“错误”的行为,请考虑提交包含全面复制步骤和示例项目的 Radar。

于 2014-07-15T19:08:00.453 回答
1

根据我上面的一些评论,简短的回答是 iOS7 不像 VoiceOver 那样支持用于切换控制的各种回调和 API。适用于 VoiceOver 的不一定适用于 Switch Control。

向 Apple 提交了一个错误并在那里与他们进行了交互,针对 iOS8 测试我的代码表明,对 Switch Control 的支持在 beta 5 中得到了改进,但仍然不完整。accessibilityElementDidBecomeFocused 回调现在似乎可以工作,但是 Switch Control 仍然拒绝使用 accessibilityElementLanguage 回调。

于 2014-08-20T00:52:24.603 回答