gpt4 book ai didi

ios - 未调用 UIAccessibilityElement 回调

转载 作者:行者123 更新时间:2023-11-28 22:02:41 26 4
gpt4 key购买 nike

我有一个类,它是 UIAccessibilityElement 的子类,目的是在我的 Cocos2D 应用程序中启用辅助功能。

在运行时,这个类的实例都可以通过 iOS 的辅助功能进行导航,这很好,但我有一个特定的需要能够更改 iOS 使用的语言和口音(这只适用于 iOS7+ ) 对于每个元素。

我注意到要做到这一点我应该覆盖:

- (NSString*) accessibilityElementLanguage;

消息,我已经完成了。

我已经通过模拟器中的辅助功能检查器确认返回了正确的语言 ID(它显示在检查器中)。

然而,当画外音朗读与该元素关联的文本时,它坚持使用默认的设备语音和语言,因此西类牙语单词听起来完全不正确是可以理解的。

我不确定我做错了什么。当语言设置失败时,我尝试了一种解决方法,即我将处理 accessibilityElementDidBecomeFocused 回调并使用我自己的代码读取文本,但现在我什至没有获得焦点事件的回调,除非我使用可访问性检查器已启用,我触摸了我的元素之一。

我确定我这里有一些简单的错误。任何帮助将不胜感激。

类接口(interface)的代码是:

#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 节点的协议(protocol)是:

#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

最佳答案

这可能会让您感到惊讶和害怕,但即使是 Apple 也有漏洞。如果您遇到意外或主观上“错误”的行为,请考虑 filing a Radar 具有全面的复制步骤和示例项目。

关于ios - 未调用 UIAccessibilityElement 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24731226/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com