PrefaceGraphs are one of those complicated things when it comes to accessibility. iOS 15 introduces a new feature called Audio Graphs. Next we’ll learn how to build an audio representation for any SwiftUI view by using the accessibilityChartDescriptor view modifier, rendering a chart like a custom bar chart view or an image. DataPoint StructureLet’s start by building a simple bar chart view in SwiftUI that displays a set of data points using vertical bars. Here we have a DataPoint structure that describes a bar in a bar chart view. It has an id, a label, a value, and a fill color. BarChartView StructureNext, we can define a bar chart view that accepts a set of DataPoint structure instances and displays them. As shown in the example above, we have a BarChartView that takes a set of DataPoint instances and displays them as rounded rectangles of varying heights in a horizontal stack. ContentView StructureWe can easily build a bar chart view in SwiftUI. Next, let’s try using our new BarChartView with some sample data. Here, we create a sample array of DataPoint instances and pass it to the BarChartView. We also create an accessible element for the chart and disable accessibility information for its child elements. To improve the accessibility experience of the chart view, we also add accessibility labels. Finally, we can start implementing the audio graph functionality for our bar chart view. The audio graph is available through the knob menu. To use the knob, rotate two fingers on the screen of your iOS device as if you were turning a dial. VoiceOver will speak the first knob option. Continue rotating your fingers to hear more options. Release your fingers to select the audio graph. Then slide your fingers up and down on the screen to navigate. Audio charts allow users to understand and interpret chart data using audio components. VoiceOver plays sounds with different pitches as you move over bars in the chart view. VoiceOver uses high pitches for larger values and low pitches for smaller values. These tones represent the data in the array. Implementing the protocolNow, we can discuss how to implement this functionality in BarChartView. First, we must create a type that conforms to the AXChartDescriptorRepresentable protocol. The AXChartDescriptorRepresentable protocol has only one requirement, which is to create an instance of the AXChartDescriptor type. An instance of the AXChartDescriptor type represents the data in our chart, presented in a format that VoiceOver can understand and interact with. All we need to do is conform to the AXChartDescriptorRepresentable protocol and add a makeChartDescriptor function that returns an instance of AXChartDescriptor. First, we define the X and Y axes by using the AXCategoricalDataAxisDescriptor and AXNumericDataAxisDescriptor types. We want to use string labels on the X axis, that's why we use the AXCategoricalDataAxisDescriptor type. In the case of a line chart, we will use the AXNumericDataAxisDescriptor type on both axes. Implementing Line ChartNext, we define the points in the chart using the AXDataSeriesDescriptor type. There is an isContinuous parameter that allows us to define different chart styles. For example, for a bar chart it should be false, while for a line chart it should be true. As a last step, we use the accessibilityChartDescriptor view modifier to set an instance conforming to the AXChartDescriptorRepresentable protocol as the one describing our chart. Example screenshots: SummarizeThe audio chart feature is a major improvement for visually impaired users. The great thing about the audio chart feature is that you can use it with any view you want, even image views. Just create an instance of the AXChartDescriptor type. |
<<: AndroidManifest file introduction and merge conflict rules
>>: More modern image format WebP usage and conversion
Unlike traditional industries that can bring prod...
When I was a kid, I went to the mall with my pare...
The full text will cover some of Yanxuan’s models...
Yang Jingyi's 14 body charm training courses ...
Zuckerberg once said, "We are building the c...
[[161452]] Human senses are divided into vision, ...
Community introduction: How to quickly get more t...
On March 16, at the China Electric Vehicle Hundre...
As the successor to Volkswagen's flagship sed...
What is user churn ? This can be judged by the TA...
We all know that we need to brush our teeth frequ...
With the rapid development of information technol...
Those who have done Baidu bidding know that when ...
Suning's notebook summer promotion continues ...