SwipeToDismiss.swift (1515B)
1 // 2 // SwipeToDismiss.swift 3 // damus 4 // 5 // Created by Joel Klabo on 1/18/23. 6 // 7 8 import SwiftUI 9 10 struct SwipeToDismissModifier: ViewModifier { 11 let minDistance: CGFloat? 12 var onDismiss: () -> Void 13 @State private var offset: CGSize = .zero 14 @GestureState private var viewOffset: CGSize = .zero 15 16 let threshold_offset: CGFloat = 100.0 17 let minimum_opacity: CGFloat = 0.1 18 19 func body(content: Content) -> some View { 20 content 21 .offset(y: viewOffset.height) 22 .animation(.interactiveSpring(), value: viewOffset) 23 .opacity(max(min(1.0 - (abs(offset.height) / threshold_offset), 1.0), minimum_opacity)) 24 .simultaneousGesture( 25 DragGesture(minimumDistance: minDistance ?? 10) 26 .updating($viewOffset, body: { value, gestureState, transaction in 27 gestureState = CGSize(width: value.location.x - value.startLocation.x, height: value.location.y - value.startLocation.y) 28 }) 29 .onChanged { gesture in 30 if gesture.translation.width < 50 { 31 offset = gesture.translation 32 } 33 } 34 .onEnded { _ in 35 if abs(offset.height) > threshold_offset { 36 onDismiss() 37 } else { 38 offset = .zero 39 } 40 } 41 ) 42 } 43 }