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 their appearance in real-time? In SwiftUI, views are not objects with delegate methods that will get invoked when a user action takes place, as they are in UIKit; rather, SwiftUI views get rapidly re-created...