Gestures are an essential part of creating interactive and engaging user interfaces in iOS. SwiftUI provides a powerful, declarative approach to handling common gestures like taps, long presses, swipes, and drags. In this article, we’ll cover how to add each of these gestures to your SwiftUI views.
Tap Gesture
The tap gesture is the simplest and most commonly used gesture, often used for triggering actions with a single tap on a view. To use a tap gesture, apply the .onTapGesture
modifier to the view:
import SwiftUI
struct ContentView: View {
@State private var isTapped = false
var body: some View {
Circle()
.fill(isTapped ? Color.green : Color.blue)
.frame(width: 100, height: 100)
.onTapGesture {
isTapped.toggle()
}
}
}
Long Press Gesture
Continue reading
Access to all the content with our plans.
- Junior level content
- Senior level content
- Expert level content
- Extra content
- Question submissions
Monthly
Yearly
Be the first to comment