Dynamic Availability Label for CALayer

How to make CALayer available? In particular, I want the layer to be able to change its label "on the fly", as it can change at any time. The official documentation of the sample code does not actually allow this.

+2
source share
1 answer

It is further assumed that you have a supervisor for which everything has the AccessableLayer class, but if you have a more complex layout, this scheme can be modified to deal with this.

To make CALayer accessible, you need a parent view that implements the UIAccessibilityContainer methods. Here is one suggested way to do this.

Each layer has its own UIAccessibilityElement

 @interface AccessableLayer : CALayer @property (nonatomic) UIAccessibilityElement *accessibilityElement; @end 

now in your implementation you change an element whenever it changes:

 @implementation AccessableLayer ... self.accessibilityElement.accessibilityLabel = text; @end 

AccessableLayer never creates a UIAccessibilityElement because the constructor requires a UIAccessibilityContainer. So create a superview and assign it:

 #pragma mark - accessibility // The container itself is not accessible, so return NO - (BOOL)isAccessibilityElement { return NO; } // The following methods are implementations of UIAccessibilityContainer protocol methods. - (NSInteger)accessibilityElementCount { return [self.layer.sublayers count]; } - (id)accessibilityElementAtIndex:(NSInteger)index { AccessableLayer *panel = [self.layer.sublayers objectAtIndex:index]; UIAccessibilityElement *element = panel.accessibilityElement; if (element == nil) { element = [[UIAccessibilityElement alloc] initWithAccessibilityContainer:self]; element.accessibilityFrame = [self convertRect:panel.frame toView:[UIApplication sharedApplication].keyWindow]; element.accessibilityTraits = UIAccessibilityTraitButton; element.accessibilityHint = @"some hint"; element.accessibilityLabel = @"some text"; panel.accessibilityElement = element; } return element; } - (NSInteger)indexOfAccessibilityElement:(id)element { int numElements = [self accessibilityElementCount]; for (int i = 0; i < numElements; i++) { if (element == [self accessibilityElementAtIndex:i]) { return i; } } return NSNotFound; } 
+4
source

Source: https://habr.com/ru/post/1444388/


All Articles