Get your own website
Demo.swift
ContentView.swift
App.swift
 
import SwiftUI

struct DragDemo: View {
  @State private var offset: CGSize = .zero
  var body: some View {
    Text("Drag me")
      .padding(12)
      .background(.blue.opacity(0.1))
      .cornerRadius(8)
      .offset(offset)
      .gesture(
        DragGesture()
          .onChanged { value in offset = value.translation }
          .onEnded { _ in withAnimation(.spring()) { offset = .zero } }
      )
  }
}

                    
import SwiftUI

struct ContentView: View {
  var body: some View {
    DragDemo()
  }
}

                    
import SwiftUI

@main
struct MyApp: App {
  var body: some Scene {
    WindowGroup { ContentView() }
  }
}