3

Adding Interactivity to a SwiftUI View

So far, we have explored how to create complex views in SwiftUI by adding more elements, but all these views have been static; that is, they don’t respond to user interaction such as tap events. This limitation would be just acceptable if we only needed to draw pretty pixels on a user’s mobile screen, but we normally want those pretty pixels to respond dynamically to the user’s interactions and be “responsive.” By user interaction, we mean whatever gestures the user may perform in order to control the UI (e.g., tapping on a button, pinches, sliding, etc.).

In SwiftUI, views are created by means of structs, which are normally immutable. So, how can we make these views respond to user actions and change ...

Get An iOS Developer's Guide to SwiftUI now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.