Skip to content

Instantly share code, notes, and snippets.

@DavidPiper94
DavidPiper94 / MultipleDataSeries.swift
Created December 29, 2021 20:03
Example code for article about Audio Graphs - Showing multiple data series
private var series: [AXDataSeriesDescriptor] {
// 1
let yValuesSeries1 = [4.0, 5.0, 6.0, 3.0, 2.0, 1.0, 1.0, 3.0, 6.0, 9.0]
let dataPointsSeries1 = yValuesSeries1.enumerated().map { index, yValue in
AXDataPoint(x: Double(index), y: yValue)
}
// 2
let yValuesSeries2 = [6.0, 5.0, 4.0, 7.0, 8.0, 9.0, 9.0, 7.0, 4.0, 1.0]
let dataPointsSeries2 = yValuesSeries2.enumerated().map { index, yValue in
@DavidPiper94
DavidPiper94 / ChartDescriptor.swift
Created December 27, 2021 20:41
Example code for article about Audio Graphs - Putting all descriptors together
// 1
var accessibilityChartDescriptor: AXChartDescriptor? {
// 2
get {
AXChartDescriptor(
title: "Example Graph",
summary: "This graph shows example data.",
xAxis: xAxis,
yAxis: yAxis,
series: series
@DavidPiper94
DavidPiper94 / DataSeries.swift
Created December 27, 2021 20:41
Example code for article about Audio Graphs - Describing the data series
private var series: [AXDataSeriesDescriptor] {
// 1
let yValuesSeries = [4.0, 5.0, 6.0, 3.0, 2.0, 1.0, 1.0, 3.0, 6.0, 9.0]
let dataPointsSeries = yValuesSeries.enumerated().map { index, yValue in
AXDataPoint(x: Double(index), y: yValue)
}
// 2
return [
AXDataSeriesDescriptor(
@DavidPiper94
DavidPiper94 / yAxis.swift
Created December 27, 2021 20:41
Example code for article about Audio Graphs - Describing the y axis
private var yAxis: AXNumericDataAxisDescriptor {
AXNumericDataAxisDescriptor(
title: "The y axis",
range: (0...9),
gridlinePositions: [],
valueDescriptionProvider: { (value: Double) -> String in
"\(value)"
}
)
}
@DavidPiper94
DavidPiper94 / xAxis.swift
Created December 27, 2021 20:41
Example code for article about Audio Graphs - Describing the x axis
private var xAxis: AXNumericDataAxisDescriptor {
// 1
AXNumericDataAxisDescriptor(
title: "The x axis",
// 2
range: (0...9),
// 3
gridlinePositions: [],
// 4
valueDescriptionProvider: { (value: Double) -> String in
@DavidPiper94
DavidPiper94 / HistoryService.swift
Created April 7, 2021 05:46
Example code for article about UndoManager - Managing a History Part 3
class HistoryService {
// ...
// 6
func record(command: Command) {
// Remove all undone actions at top of list.
history = Array(history.drop { $0.isUndone} )
// Prepend command.
history = [command] + history
@DavidPiper94
DavidPiper94 / HistoryService.swift
Last active April 7, 2021 05:46
Example code for article about UndoManager - Managing a History Part 2
class HistoryService {
// ...
// 4
func registerUndoFor(command: Command) {
undoManager.registerUndo(withTarget: self) { selfTarget in
command.isUndone = true
command.reverse()
selfTarget.registerRedoFor(command: command)
@DavidPiper94
DavidPiper94 / RecordingCommands.swift
Created March 18, 2021 05:58
Example code for article about UndoManager - Recording Commands
// 1
func redButtonPressed(_ sender: UIButton) {
recordColorChangeCommand(newColor: .red)
}
// 2
func recordColorChangeCommand(newColor: UIColor) {
let currentColor = colorView.backgroundColor
let command = Command(
execute: { self.colorView.backgroundColor = newColor },
@DavidPiper94
DavidPiper94 / History.swift
Last active April 7, 2021 05:47
Example code for article about UndoManager - Managing a History
class HistoryService {
// 1
let undoManager = UndoManager()
var history = [Command]()
// 2
var canUndo: Bool { undoManager.canUndo }
func undo() {
guard canUndo else { return }
@DavidPiper94
DavidPiper94 / Command.swift
Created March 18, 2021 05:58
Example code for article about UndoManager - Command
class Command {
var isUndone: Bool
let execute: () -> Void
let reverse: () -> Void
init(execute: @escaping () -> Void, reverse: @escaping () -> Void) {
isUndone = false
self.execute = execute
self.reverse = reverse
}