GestureSwiftUI

Projet Simplon - Apple Foundation Extended sur les gestures en SwiftUI

View on GitHub

DrageGestureDemo


import SwiftUI

struct DragGestureDemo: View {
    @State private var position = CGSize.zero
    @State private var estEnTrainDeGlisser = false

    var body: some View {
        VStack {
            Text("Glissez le cercle")
                .font(.headline)

            Spacer()

            Circle()
                .fill(estEnTrainDeGlisser ? Color.orange : Color.blue)
                .frame(width: 100, height: 100)
                .offset(x: position.width, y: position.height)
                .gesture(
                    DragGesture()
                        .onChanged { valeur in
                            estEnTrainDeGlisser = true
                            position = valeur.translation
                        }
                        .onEnded { valeur in
                            estEnTrainDeGlisser = false
                            position = .zero

                        }
                )

            Spacer()

            Text("X: (Int(position.width)), Y: (Int(position.height))")
                .foregroundColor(.gray)
        }
    }
}

#Preview {
    DragGestureDemo()
}