Add handling layer backed accessibility elements within layer backed nodes

This commit is contained in:
Michael Schneider 2016-04-11 12:59:04 -07:00
parent a6e66fa5d7
commit 2fade63f1b
3 changed files with 38 additions and 34 deletions

View File

@ -26,7 +26,7 @@
__unsafe_unretained ASDisplayNode *_node; // Though UIView has a .node property added via category, since we can add an ivar to a subclass, use that for performance. __unsafe_unretained ASDisplayNode *_node; // Though UIView has a .node property added via category, since we can add an ivar to a subclass, use that for performance.
BOOL _inHitTest; BOOL _inHitTest;
BOOL _inPointInside; BOOL _inPointInside;
NSMutableArray *_accessibleElements; NSArray *_accessibleElements;
} }
@synthesize asyncdisplaykit_node = _node; @synthesize asyncdisplaykit_node = _node;

View File

@ -11,9 +11,6 @@
#import "ASDisplayNodeExtras.h" #import "ASDisplayNodeExtras.h"
#import "ASDisplayNode+FrameworkPrivate.h" #import "ASDisplayNode+FrameworkPrivate.h"
#import <objc/runtime.h>
#pragma mark - UIAccessibilityElement #pragma mark - UIAccessibilityElement
@implementation UIAccessibilityElement (_ASDisplayView) @implementation UIAccessibilityElement (_ASDisplayView)
@ -34,10 +31,27 @@
#pragma mark - _ASDisplayView / UIAccessibilityContainer #pragma mark - _ASDisplayView / UIAccessibilityContainer
@interface _ASDisplayView () { static NSArray *ASCollectUIAccessibilityElementsForNode(ASDisplayNode *viewNode, ASDisplayNode *subnode, id container) {
NSMutableArray *_accessibleElements; NSMutableArray *accessibleElements = [NSMutableArray array];
ASDisplayNodePerformBlockOnEveryNodeBFS(subnode, ^(ASDisplayNode * _Nonnull currentNode) {
// For every subnode that is layer backed or it's supernode has shouldRasterizeDescendants enabled
// we have to create a UIAccessibilityElement as no view for this node exists
if (currentNode != viewNode && currentNode.isAccessibilityElement) {
UIAccessibilityElement *accessibilityElement = [UIAccessibilityElement accessibilityElementWithContainer:container node:currentNode];
// As the node hierarchy is flattened it's necessary to convert the frame for each subnode in the tree to the
// coordinate system of the supernode
CGRect frame = [viewNode convertRect:currentNode.bounds fromNode:currentNode];
accessibilityElement.accessibilityFrame = UIAccessibilityConvertFrameToScreenCoordinates(frame, container);
[accessibleElements addObject:accessibilityElement];
}
});
return [accessibleElements copy];
} }
@interface _ASDisplayView () {
NSArray *_accessibleElements;
}
@end @end
@implementation _ASDisplayView (UIAccessibilityContainer) @implementation _ASDisplayView (UIAccessibilityContainer)
@ -46,51 +60,41 @@
- (NSArray *)accessibleElements - (NSArray *)accessibleElements
{ {
ASDisplayNode *selfNode = self.asyncdisplaykit_node; ASDisplayNode *viewNode = self.asyncdisplaykit_node;
if (selfNode == nil) { if (viewNode == nil) {
return nil; return nil;
} }
_accessibleElements = [[NSMutableArray alloc] init];
// Handle rasterize case // Handle rasterize case
if (selfNode.shouldRasterizeDescendants) { if (viewNode.shouldRasterizeDescendants) {
// If the node has shouldRasterizeDescendants enabled it's necessaty to go through the whole subnodes _accessibleElements = ASCollectUIAccessibilityElementsForNode(viewNode, viewNode, self);
// tree of the node in BFS fashion and create for all subnodes UIAccessibilityElement objects ourselves
// as the view hierarchy is flattened
ASDisplayNodePerformBlockOnEveryNodeBFS(selfNode, ^(ASDisplayNode * _Nonnull node) {
// For every subnode we have to create a UIAccessibilityElement as we cannot just add the view to the
// accessibleElements as for a subnode of a node with shouldRasterizeDescendants enabled no view exists
if (node != selfNode && node.isAccessibilityElement) {
UIAccessibilityElement *accessibilityElement = [UIAccessibilityElement accessibilityElementWithContainer:self node:node];
// As the node hierarchy is flattened it's necessary to convert the frame for each subnode in the tree to the
// coordinate system of the node with shouldRasterizeDescendants enabled
CGRect frame = [selfNode convertRect:node.bounds fromNode:node];
accessibilityElement.accessibilityFrame = UIAccessibilityConvertFrameToScreenCoordinates(frame, self);
[_accessibleElements addObject:accessibilityElement];
}
});
return _accessibleElements; return _accessibleElements;
} }
// Handle not rasterize case // Handle not rasterize case
// Create UI accessiblity elements for each subnode that represent an elment within the accessibility container NSMutableArray *accessibleElements = [NSMutableArray array];
for (ASDisplayNode *subnode in selfNode.subnodes) {
for (ASDisplayNode *subnode in viewNode.subnodes) {
if (subnode.isAccessibilityElement) { if (subnode.isAccessibilityElement) {
// An accessiblityElement can either be a UIView or a UIAccessibilityElement
id accessiblityElement = nil; id accessiblityElement = nil;
if (subnode.isLayerBacked) { if (subnode.isLayerBacked) {
// The same comment for layer backed nodes is true as for subnodes within a shouldRasterizeDescendants node. // No view for layer backed nodes exist. It's necessary to create a UIAccessibilityElement that represents this node
// See details above
accessiblityElement = [UIAccessibilityElement accessibilityElementWithContainer:self node:subnode]; accessiblityElement = [UIAccessibilityElement accessibilityElementWithContainer:self node:subnode];
} else { } else {
accessiblityElement = subnode.view; accessiblityElement = subnode.view;
} }
[accessiblityElement setAccessibilityFrame:UIAccessibilityConvertFrameToScreenCoordinates(subnode.frame, self)]; [accessiblityElement setAccessibilityFrame:UIAccessibilityConvertFrameToScreenCoordinates(subnode.frame, self)];
[_accessibleElements addObject:accessiblityElement]; [accessibleElements addObject:accessiblityElement];
} else if ([subnode accessibilityElementCount] > 0) { // Check if it's an UIAccessibilityContainer } else if (subnode.isLayerBacked) {
[_accessibleElements addObject:subnode.view]; // Go down the hierarchy of the layer backed subnode and collect all of the UIAccessibilityElement
[accessibleElements addObjectsFromArray:ASCollectUIAccessibilityElementsForNode(viewNode, subnode, self)];
} else if ([subnode accessibilityElementCount] > 0) {
// Add UIAccessibilityContainer
[accessibleElements addObject:subnode.view];
} }
} }
_accessibleElements = [accessibleElements copy];
return _accessibleElements; return _accessibleElements;
} }

View File

@ -721,7 +721,7 @@ if (shouldApply) { _layer.layerProperty = (layerValueExpr); } else { ASDisplayNo
// Helper function with following logic: // Helper function with following logic:
// - If the node is not loaded yet use the property from the pending state // - If the node is not loaded yet use the property from the pending state
// - In case the node is loaded // - In case the node is loaded
// - Check if the node has a view and get the // - Check if the node has a view and get the value from the view if loaded or from the pending state
// - If view is not available, e.g. the node is layer backed return the property value // - If view is not available, e.g. the node is layer backed return the property value
#define _getAccessibilityFromViewOrProperty(nodeProperty, viewAndPendingViewStateProperty) __loaded(self) ? \ #define _getAccessibilityFromViewOrProperty(nodeProperty, viewAndPendingViewStateProperty) __loaded(self) ? \
(_view ? _view.viewAndPendingViewStateProperty : nodeProperty )\ (_view ? _view.viewAndPendingViewStateProperty : nodeProperty )\