1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import SwiftUI
/// Shared handle so StoryViewer can trigger a fade-dismiss programmatically
/// (e.g. when all stories finish) using the same UIKit path as the drag gesture.
@MainActor
final class FadeDismissHandle {
fileprivate weak var targetView: UIView?
fileprivate var performDismiss: (() -> Void)?
func fadeDismiss() {
guard let view = targetView else {
performDismiss?()
return
}
UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut, animations: {
view.alpha = 0
view.transform = CGAffineTransform(scaleX: 0.92, y: 0.92)
}) { _ in
if let vc = view.findViewController(),
let presented = vc.presentedViewController ?? (vc.isBeingPresented ? vc : nil)
{
presented.dismiss(animated: false) {
view.alpha = 1
view.transform = .identity
self.performDismiss?()
}
} else {
view.isHidden = true
self.performDismiss?()
}
}
}
}
/// Installs a UIKit UIPanGestureRecognizer on the hosting view controller's view
/// and applies CGAffineTransform directly — bypassing SwiftUI's rendering pipeline
/// for buttery smooth drag-to-dismiss.
struct DragToDismissInstaller: UIViewRepresentable {
let handle: FadeDismissHandle
let onDismiss: () -> Void
let onDragStart: () -> Void
let onDragCancel: () -> Void
let onSwipeLeft: () -> Void
let onSwipeRight: () -> Void
var onSwipeUp: (() -> Void)?
var onHorizontalDragStart: ((Bool) -> Void)? // true = swiping left (forward)
var onSwipeDragging: ((CGFloat) -> Void)? // raw translation.x during drag
var onHorizontalDragCancel: (() -> Void)?
/// When false, the pan gesture is disabled so another presented view (e.g.
/// a comment sheet) can own touches without interference.
var isEnabled: Bool = true
func makeUIView(context: Context) -> UIView {
let view = UIView(frame: .zero)
view.backgroundColor = .clear
view.isUserInteractionEnabled = false
context.coordinator.anchorView = view
DispatchQueue.main.async {
context.coordinator.installGestureIfNeeded()
}
return view
}
func updateUIView(_: UIView, context: Context) {
context.coordinator.onDismiss = onDismiss
context.coordinator.onDragStart = onDragStart
context.coordinator.onDragCancel = onDragCancel
context.coordinator.onSwipeLeft = onSwipeLeft
context.coordinator.onSwipeRight = onSwipeRight
context.coordinator.onSwipeUp = onSwipeUp
context.coordinator.onHorizontalDragStart = onHorizontalDragStart
context.coordinator.onSwipeDragging = onSwipeDragging
context.coordinator.onHorizontalDragCancel = onHorizontalDragCancel
context.coordinator.setEnabled(isEnabled)
handle.performDismiss = onDismiss
}
func makeCoordinator() -> Coordinator {
Coordinator(
handle: handle,
onDismiss: onDismiss,
onDragStart: onDragStart,
onDragCancel: onDragCancel,
onSwipeLeft: onSwipeLeft,
onSwipeRight: onSwipeRight,
onHorizontalDragStart: onHorizontalDragStart,
onSwipeDragging: onSwipeDragging,
onHorizontalDragCancel: onHorizontalDragCancel
)
}
private enum DragDirection {
case none, vertical, horizontal
}
final class Coordinator: NSObject, UIGestureRecognizerDelegate {
let handle: FadeDismissHandle
var onDismiss: () -> Void
var onDragStart: () -> Void
var onDragCancel: () -> Void
var onSwipeLeft: () -> Void
var onSwipeRight: () -> Void
var onSwipeUp: (() -> Void)?
var onHorizontalDragStart: ((Bool) -> Void)?
var onSwipeDragging: ((CGFloat) -> Void)?
var onHorizontalDragCancel: (() -> Void)?
weak var anchorView: UIView?
private weak var targetView: UIView?
private var panGesture: UIPanGestureRecognizer?
private var direction: DragDirection = .none
init(
handle: FadeDismissHandle,
onDismiss: @escaping () -> Void,
onDragStart: @escaping () -> Void,
onDragCancel: @escaping () -> Void,
onSwipeLeft: @escaping () -> Void,
onSwipeRight: @escaping () -> Void,
onHorizontalDragStart: ((Bool) -> Void)? = nil,
onSwipeDragging: ((CGFloat) -> Void)? = nil,
onHorizontalDragCancel: (() -> Void)? = nil
) {
self.handle = handle
self.onDismiss = onDismiss
self.onDragStart = onDragStart
self.onDragCancel = onDragCancel
self.onSwipeLeft = onSwipeLeft
self.onSwipeRight = onSwipeRight
onSwipeUp = nil
self.onHorizontalDragStart = onHorizontalDragStart
self.onSwipeDragging = onSwipeDragging
self.onHorizontalDragCancel = onHorizontalDragCancel
}
func installGestureIfNeeded() {
guard panGesture == nil, let anchor = anchorView else { return }
guard let vc = anchor.findViewController() else { return }
let target = vc.view!
targetView = target
handle.targetView = target
handle.performDismiss = onDismiss
let pan = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
pan.delegate = self
target.addGestureRecognizer(pan)
panGesture = pan
}
func setEnabled(_ enabled: Bool) {
panGesture?.isEnabled = enabled
}
/// Allow SwiftUI tap gestures to work simultaneously
func gestureRecognizer(_: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith other: UIGestureRecognizer) -> Bool {
!(other is UIPanGestureRecognizer)
}
@objc private func handlePan(_ gesture: UIPanGestureRecognizer) {
guard let view = targetView else { return }
let translation = gesture.translation(in: view.superview ?? view)
let velocity = gesture.velocity(in: view.superview ?? view)
switch gesture.state {
case .began:
direction = .none
case .changed:
// Lock direction after enough movement
if direction == .none {
let absX = abs(translation.x)
let absY = abs(translation.y)
if absX > 15 || absY > 15 {
if absY > absX, translation.y > 0 {
direction = .vertical
onDragStart()
} else if absY > absX, translation.y < 0, onSwipeUp != nil {
direction = .vertical
} else if absX > absY {
direction = .horizontal
onHorizontalDragStart?(translation.x < 0)
}
}
}
if direction == .vertical {
let ty = max(translation.y, 0)
let progress = min(ty / 300, 1)
let scale = 1 - progress * 0.1
view.transform = CGAffineTransform(translationX: 0, y: ty)
.scaledBy(x: scale, y: scale)
view.layer.cornerRadius = progress * 24
view.clipsToBounds = true
} else if direction == .horizontal {
onSwipeDragging?(translation.x)
}
case .ended, .cancelled:
if direction == .vertical, translation.y < -80 || velocity.y < -500 {
onSwipeUp?()
direction = .none
return
}
if direction == .vertical {
let ty = max(translation.y, 0)
let dismissThreshold = view.bounds.height * 0.35
if ty > dismissThreshold || velocity.y > 1200 {
// Throw downward off screen
let screenHeight = view.bounds.height
let speed = max(velocity.y, 800)
let remaining = screenHeight - ty + 100
let duration = min(max(Double(remaining / speed), 0.15), 0.3)
UIView.animate(withDuration: duration, delay: 0, options: .curveEaseIn) {
view.transform = CGAffineTransform(translationX: 0, y: screenHeight + 100)
.scaledBy(x: 0.9, y: 0.9)
} completion: { _ in
if let vc = view.findViewController(),
let presented = vc.presentedViewController ?? (vc.isBeingPresented ? vc : nil)
{
presented.dismiss(animated: false) {
view.alpha = 1
view.transform = .identity
view.layer.cornerRadius = 0
view.clipsToBounds = false
self.onDismiss()
}
} else {
view.isHidden = true
self.onDismiss()
}
}
} else {
// Spring back
UIView.animate(withDuration: 0.35, delay: 0, usingSpringWithDamping: 0.85, initialSpringVelocity: 0, options: [.allowUserInteraction]) {
view.transform = .identity
view.layer.cornerRadius = 0
view.clipsToBounds = false
}
onDragCancel()
}
} else if direction == .horizontal {
if translation.x < -80 || velocity.x < -500 {
onSwipeLeft()
} else if translation.x > 80 || velocity.x > 500 {
onSwipeRight()
} else {
onHorizontalDragCancel?()
}
}
direction = .none
default:
break
}
}
}
}
private extension UIView {
func findViewController() -> UIViewController? {
var responder: UIResponder? = self
while let next = responder?.next {
if let vc = next as? UIViewController {
return vc
}
responder = next
}
return nil
}
}
#Preview {
// DragToDismiss is a UIKit-backed gesture modifier — preview shows it applied.
@Previewable @State var dismissed = false
ZStack {
Color.black.ignoresSafeArea()
Text(dismissed ? "Dismissed" : "Drag down to dismiss")
.foregroundStyle(.white)
}
}