← All daysDay 22 of 49 · Part 6: Drawing and animation
Day 22

Canvas

These are the lesson notes for this day. Its build-along tutorial and flash cards have not been written yet.

Lesson 25

Canvas

Canvas hands you a drawing context and a size, and you draw straight away — paths, text, images — without creating a view for each thing drawn. Use it when you have hundreds or thousands of items to draw, such as a chart, particles, or a waveform, where a view for each one would be too slow. Pair it with TimelineView, which redraws on a schedule, to animate.

The trade-off is that things drawn in a Canvas are not views. They have no accessibility elements, gestures, or state of their own, so you provide those for the canvas as a whole. For standard charts, use the Swift Charts framework instead.

Exercise

Plot 200 data points as a line chart in a Canvas, with axis lines. Add an accessibility label that describes the chart.

After this lesson you should be able to answer
  1. When should you choose Canvas over many small views?
  2. What do you give up by drawing in a Canvas?
  3. What is TimelineView for?
Resources