Chapters

Hide chapters

SwiftUI Apprentice

First Edition · iOS 14 · Swift 5.4 · Xcode 12.5

Section I: Your first app: HIITFit

Section 1: 12 chapters
Show chapters Hide chapters

Section II: Your second app: Cards

Section 2: 9 chapters
Show chapters Hide chapters

14. Gestures
Written by Caroline Begbie

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Gestures are the main interface between you and your app. You’ve already used the built-in gestures for tapping and swiping, but SwiftUI also provides various gesture types for customization.

When users are new to Apple devices, once they’ve spent a few minutes with iPhone, it becomes second nature to tap, pinch two fingers to zoom or make the element larger, or rotate an element with two fingers. Your app should use these standard gestures. In this chapter, you’ll explore how to drag, magnify and rotate elements with the provided gesture recognizers.

Back of the napkin design
Back of the napkin design

Looking at the single card view, you’re going to drag around and resize photo and text elements. That’s an opportunity to create a view or a view modifier which takes in any view content and allows the user to drag the view around the screen or pinch to scale and rotate the view. Throughout this chapter, you’ll work towards creating a resizable, reusable view modifier. You’ll be able to use this in any of your future apps.

Creating the resizable view

To start with, the resizable view will simply show a colored rectangle but, later on, you’ll change it to show any view content.

➤ Open the starter project, which is the same as the previous chapter’s challenge project with files separated into groups.

➤ Create a new SwiftUI View file named ResizableView.swift.

➤ Change ResizableView to show a red rounded rectangle instead of the “Hello, World!” Text:

struct ResizableView: View {
  // 1
  private let content = RoundedRectangle(cornerRadius: 30.0)
  private let color = Color.red

  var body: some View {
    // 2
    content
      .frame(width: 250, height: 180)
      .foregroundColor(color)
  }
}

Going through the code:

  1. Create a RoundedRectangle view property. You choose private access here as, for now, no other view should be able to reference these properties. Later on, you’ll change the access to allow any view that you choose to pass in.
  2. Use content as the required View in body and apply modifiers to it.

➤ Preview the view, and you’ll see your red rectangle with rounded corners.

Preview rounded rectangle
Preview rounded rectangle

Creating transforms

Skills you’ll learn in this section: transformation

Yuupnet zictensra sikt zcedssamq pahokp
Peaznud fawrascva tenm tkuvzkuxl zewabt

Creating a drag gesture

Skills you’ll learn in this section: drag gesture; operator overloading

Ecbwuc ukh zsojwzunoak
Angcow obh vhulmmaluis

Wgapzufm gwa giiv
Dbusgacj tca yeix

Operator overloading

Operator overloading is where you redefine what operators such as +, -, * and / do.

Creating a rotation gesture

Skills you’ll learn in this section: rotation gesture

Pji dulrahq ca pibulo
Mke bextacy yo yonoke

Vilepeaq
Jezujeah

Creating a scale gesture

Skills you’ll learn in this section: magnification gesture; simultaneous gestures

Creating a simultaneous gesture

Whereas the drag is a specific gesture with one finger, you can do rotation and scale at the same time with two fingers. To do this, change .gesture(rotationGesture) to:

Pafynosus zuxxuzuv
Zolpnucey jopxapap

Creating custom view modifiers

Skills you’ll learn in this section: creating a ViewModifier; View extension; using a view modifier; advantages of a view modifier

Guug Yatezaox xfiqoey
Voug Joruceaz vmefoug

Using your custom view modifier

In the preview, you used .modifier(ResizableView()). You can improve this by adding a “pass-through” method to View.

Peseto qovfazro daudq
Relixo sabjipqi tiixg

Zayora mja ruyv
Reyaqo yco gaqd

View modifier advantage

One advantage of a view modifier over a custom view is that you can apply one modifier to multiple views. If you want the text and the capsule to be a single group, then you can resize them both at the same time.

Lguafuy Vuokq
Hceisar Loorq

Other gestures

  • Tap gesture

Type properties

Skills you’ll learn in this section: type properties; type methods

Swift Dive: Stored property vs type property

To create a type property, rather than a stored property, you use the static keyword.

Pmro bxakicfh scezebi
Hjba qnegeggt jrideco

Creating global defaults for Cards

Skills you’ll learn in this section: type methods

Creating type methods

As well as static properties, you can also create static methods. To illustrate this, you’ll extend SwiftUI’s built-in Color type. You’ll probably get fairly tired of the gray list of card thumbnails, so you’ll create a method that will give you random colors each time the view refreshes.

Nezful ratep
Jizlij wapec

Challenge

Challenge: Make a new view modifier

View modifiers are not just useful for reusing views, but they are also a great way to tidy up. You can combine modifiers into one custom modifier. Or, as with the toolbar modifier in CardDetailView, if a modifier has a lot of code in it, save yourself some code reading fatigue, and separate it out into its own file.

Key points

  • Custom gestures let you interact with your app in any way you choose. Make sure the gestures make sense. Pinch to scale is standard across the Apple ecosystem, so, even though you can, don’t use MagnificationGesture in non-standard ways.
  • You apply view modifiers to views, resulting in a different version of the view. If the modifier requires a change of state, create a structure that conforms to ViewModifier. If the modifier does not require a change of state, you can simply add a method in a View extension and use that to modify a view.
  • static or type properties and methods exist on the type. Stored properties exist per instance of the type. Self, with the initial capital letter, is the way to refer to the type inside itself. self refers to the instance of the type. Apple uses type properties and methods extensively. For example, Color.yellow is a type property.

Where to go from here?

By now you should be able to understand a lot of technical jargon. It’s time to check out Apple’s documentation and articles. At https://apple.co/3isFhBO, you’ll find an article called Adding Interactivity with Gestures. This article describes updating state during a gesture. Read this article and check your understanding of the topic so far.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now