← All daysDay 26 of 49 · Part 7: Input and interaction
Day 26

Gestures

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

Lesson 30

Gestures

The simple cases are .onTapGesture and .onLongPressGesture. For anything more, create a gesture value — DragGesture, MagnifyGesture, RotateGesture, LongPressGesture — and attach it with .gesture(). A gesture reports values through .onChanged and .onEnded. @GestureState holds a value that resets itself automatically when the gesture ends, which makes it ideal for a temporary drag offset. You combine gestures with .simultaneously(with:), .sequenced(before:) and .exclusively(before:).

One detail catches everyone: the area of a view that responds to touches is its visible content. Add .contentShape(Rectangle()) to make the whole frame respond.

Exercise

Make a card you can drag. When released it springs back to where it started — unless it was dragged past a threshold, in which case it is dismissed.

After this lesson you should be able to answer
  1. What is the difference between @GestureState and @State for tracking a drag?
  2. How do you require a long press before a drag can begin?
  3. Why might taps on the empty part of a row not register, and how do you fix it?
Resources