react-native-sheet2 1.1.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +79 -0
  3. package/android/build.gradle +83 -0
  4. package/android/gradle.properties +6 -0
  5. package/android/src/main/AndroidManifest.xml +12 -0
  6. package/android/src/main/java/com/behavior/BottomSheetBehavior.java +2329 -0
  7. package/android/src/main/java/com/behavior/InsetsAnimationCallback.java +83 -0
  8. package/android/src/main/java/com/modal/FullScreenDialog.kt +100 -0
  9. package/android/src/main/java/com/modal/TopModalView.kt +77 -0
  10. package/android/src/main/java/com/modal/TopModalViewManager.kt +83 -0
  11. package/android/src/main/java/com/modal/TopModalViewModule.kt +36 -0
  12. package/android/src/main/java/com/sheet/AppFittedSheet.kt +149 -0
  13. package/android/src/main/java/com/sheet/BaseRNView.kt +73 -0
  14. package/android/src/main/java/com/sheet/CustomBottomSheetDialog.java +238 -0
  15. package/android/src/main/java/com/sheet/DialogRootViewGroup.kt +159 -0
  16. package/android/src/main/java/com/sheet/FragmentModalBottomSheet.kt +54 -0
  17. package/android/src/main/java/com/sheet/ReactNativeReflection.java +24 -0
  18. package/android/src/main/java/com/sheet/SheetPackage.kt +20 -0
  19. package/android/src/main/java/com/sheet/SheetViewManager.kt +144 -0
  20. package/android/src/main/java/com/sheet/SheetViewModule.kt +43 -0
  21. package/android/src/main/java/com/sheet/utils.kt +71 -0
  22. package/android/src/main/java/com/transparent/activity/ActivityModalView.kt +71 -0
  23. package/android/src/main/java/com/transparent/activity/TransparentActivity.kt +48 -0
  24. package/android/src/main/java/com/transparent/activity/TransparentActivityManager.kt +63 -0
  25. package/android/src/main/java/com/transparent/activity/TransparentActivityModule.kt +28 -0
  26. package/android/src/main/res/anim/fade_in.xml +8 -0
  27. package/android/src/main/res/anim/fade_out.xml +11 -0
  28. package/android/src/main/res/anim/slide_down.xml +8 -0
  29. package/android/src/main/res/anim/slide_up.xml +8 -0
  30. package/android/src/main/res/layout/dialog_bottom_sheet.xml +64 -0
  31. package/android/src/main/res/layout/item_full.xml +14 -0
  32. package/android/src/main/res/values/colors.xml +15 -0
  33. package/android/src/main/res/values/styles.xml +75 -0
  34. package/ios/FittedSheets/Compatible.swift +55 -0
  35. package/ios/FittedSheets/InitialTouchPanGestureRecognizer.swift +19 -0
  36. package/ios/FittedSheets/SheetContentViewController.swift +326 -0
  37. package/ios/FittedSheets/SheetContentViewDelegate.swift +17 -0
  38. package/ios/FittedSheets/SheetOptions.swift +100 -0
  39. package/ios/FittedSheets/SheetSize.swift +20 -0
  40. package/ios/FittedSheets/SheetTransition.swift +153 -0
  41. package/ios/FittedSheets/SheetView.swift +21 -0
  42. package/ios/FittedSheets/SheetViewController.swift +761 -0
  43. package/ios/FittedSheets/SheetViewDelegate.swift +16 -0
  44. package/ios/FittedSheets/UIColorExtension.swift +59 -0
  45. package/ios/FittedSheets/UIViewControllerExtension.swift +27 -0
  46. package/ios/FittedSheets/Yalta.swift +420 -0
  47. package/ios/LBBottomSheet/BottomSheetConstant.swift +48 -0
  48. package/ios/LBBottomSheet/Model/BottomSheetController+Behavior.swift +262 -0
  49. package/ios/LBBottomSheet/Model/BottomSheetController+Theme.swift +156 -0
  50. package/ios/LBBottomSheet/Protocols/BottomSheetInteractionDelegate.swift +29 -0
  51. package/ios/LBBottomSheet/Protocols/BottomSheetPositionDelegate.swift +31 -0
  52. package/ios/LBBottomSheet/UI/BottomSheet.storyboard +105 -0
  53. package/ios/LBBottomSheet/UI/BottomSheetController.swift +601 -0
  54. package/ios/LBBottomSheet/Utils/Components/ForwardingEventsView.swift +60 -0
  55. package/ios/LBBottomSheet/Utils/Extensions/CACornerMask+Extension.swift +33 -0
  56. package/ios/LBBottomSheet/Utils/Extensions/Mirror+Extension.swift +47 -0
  57. package/ios/LBBottomSheet/Utils/Extensions/UIApplication+Extension.swift +35 -0
  58. package/ios/LBBottomSheet/Utils/Extensions/UIColor+Extension.swift +61 -0
  59. package/ios/LBBottomSheet/Utils/Extensions/UIGestureRecognizer+Extensions.swift +28 -0
  60. package/ios/LBBottomSheet/Utils/Extensions/UIScreen+Extension.swift +28 -0
  61. package/ios/LBBottomSheet/Utils/Extensions/UIScrollView+Extension.swift +25 -0
  62. package/ios/LBBottomSheet/Utils/Extensions/UIView+Extension.swift +52 -0
  63. package/ios/LBBottomSheet/Utils/Extensions/UIViewController+Extension.swift +104 -0
  64. package/ios/Sheet-Bridging-Header.h +1 -0
  65. package/ios/Sheet.xcodeproj/project.pbxproj +377 -0
  66. package/ios/SheetViewManager.m +27 -0
  67. package/ios/SheetViewManager.swift +351 -0
  68. package/ios/TopModalViewManager.swift +196 -0
  69. package/package.json +103 -0
  70. package/react-native-sheet.podspec +21 -0
  71. package/src/FittedSheet.tsx +177 -0
  72. package/src/TopModal.tsx +94 -0
  73. package/src/TransparentActivity.tsx +98 -0
  74. package/src/index.tsx +16 -0
@@ -0,0 +1,761 @@
1
+ //
2
+ // SheetViewController.swift
3
+ // FittedSheetsPod
4
+ //
5
+ // Created by Gordon Tucker on 7/29/20.
6
+ // Copyright © 2020 Gordon Tucker. All rights reserved.
7
+ //
8
+
9
+ #if os(iOS) || os(tvOS) || os(watchOS)
10
+ import UIKit
11
+
12
+ public class SheetViewController: UIViewController {
13
+ public private(set) var options: SheetOptions
14
+
15
+ /// Default value for autoAdjustToKeyboard. Defaults to true.
16
+ public static var autoAdjustToKeyboard = true
17
+ /// Automatically grow/move the sheet to accomidate the keyboard. Defaults to false.
18
+ public var autoAdjustToKeyboard = SheetViewController.autoAdjustToKeyboard
19
+
20
+ /// Default value for allowPullingPastMaxHeight. Defaults to true.
21
+ public static var allowPullingPastMaxHeight = true
22
+ /// Allow pulling past the maximum height and bounce back. Defaults to true.
23
+ public var allowPullingPastMaxHeight = SheetViewController.allowPullingPastMaxHeight
24
+
25
+ /// Default value for allowPullingPastMinHeight. Defaults to true.
26
+ public static var allowPullingPastMinHeight = true
27
+ /// Allow pulling below the minimum height and bounce back. Defaults to true.
28
+ public var allowPullingPastMinHeight = SheetViewController.allowPullingPastMinHeight
29
+
30
+ /// The sizes that the sheet will attempt to pin to. Defaults to intrinsic only.
31
+ public var sizes: [SheetSize] = [.intrinsic] {
32
+ didSet {
33
+ self.updateOrderedSizes()
34
+ }
35
+ }
36
+ public var orderedSizes: [SheetSize] = []
37
+ public private(set) var currentSize: SheetSize = .intrinsic
38
+ /// Allows dismissing of the sheet by pulling down
39
+ public var dismissOnPull: Bool = true
40
+ /// Dismisses the sheet by tapping on the background overlay
41
+ public var dismissOnOverlayTap: Bool = true
42
+ /// If true you can pull using UIControls (so you can grab and drag a button to control the sheet)
43
+ public var shouldRecognizePanGestureWithUIControls: Bool = true
44
+
45
+ /// The view controller being presented by the sheet currently
46
+ public var childViewController: UIViewController {
47
+ return self.contentViewController.childViewController
48
+ }
49
+
50
+ public override var childForStatusBarStyle: UIViewController? {
51
+ childViewController
52
+ }
53
+
54
+ public override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
55
+ return childViewController.supportedInterfaceOrientations
56
+ }
57
+
58
+ public static var hasBlurBackground = false
59
+ public var hasBlurBackground = SheetViewController.hasBlurBackground {
60
+ didSet {
61
+ blurView.isHidden = !hasBlurBackground
62
+ overlayView.backgroundColor = hasBlurBackground ? .clear : self.overlayColor
63
+ }
64
+ }
65
+
66
+ public static var minimumSpaceAbovePullBar: CGFloat = 0
67
+ public var minimumSpaceAbovePullBar: CGFloat {
68
+ didSet {
69
+ if self.isViewLoaded {
70
+ self.resize(to: self.currentSize)
71
+ }
72
+ }
73
+ }
74
+
75
+ /// The default color of the overlay background
76
+ public static var overlayColor = UIColor(white: 0, alpha: 0.25)
77
+ /// The color of the overlay background
78
+ public var overlayColor = SheetViewController.overlayColor {
79
+ didSet {
80
+ self.overlayView.backgroundColor = self.hasBlurBackground ? .clear : self.overlayColor
81
+ }
82
+ }
83
+
84
+ public static var blurEffect: UIBlurEffect = {
85
+ return UIBlurEffect(style: .prominent)
86
+ }()
87
+
88
+ public var blurEffect = SheetViewController.blurEffect {
89
+ didSet {
90
+ self.blurView.effect = blurEffect
91
+ }
92
+ }
93
+ public static var allowGestureThroughOverlay: Bool = false
94
+ public var allowGestureThroughOverlay: Bool = SheetViewController.allowGestureThroughOverlay {
95
+ didSet {
96
+ self.overlayTapView.isUserInteractionEnabled = !self.allowGestureThroughOverlay
97
+ }
98
+ }
99
+
100
+ public static var cornerRadius: CGFloat = 12
101
+ public var cornerRadius: CGFloat {
102
+ get { return self.contentViewController.cornerRadius }
103
+ set { self.contentViewController.cornerRadius = newValue }
104
+ }
105
+
106
+ @available(iOS 13.0, *)
107
+ public static var cornerCurve: CALayerCornerCurve = .circular
108
+
109
+ @available(iOS 13.0, *)
110
+ public var cornerCurve: CALayerCornerCurve {
111
+ get { return self.contentViewController.cornerCurve }
112
+ set { self.contentViewController.cornerCurve = newValue }
113
+ }
114
+
115
+ public static var gripSize: CGSize = CGSize (width: 50, height: 6)
116
+ public var gripSize: CGSize {
117
+ get { return self.contentViewController.gripSize }
118
+ set { self.contentViewController.gripSize = newValue }
119
+ }
120
+
121
+ public static var gripColor: UIColor = UIColor(white: 0.868, black: 0.1)
122
+ public var gripColor: UIColor? {
123
+ get { return self.contentViewController.gripColor }
124
+ set { self.contentViewController.gripColor = newValue }
125
+ }
126
+
127
+ public static var pullBarBackgroundColor: UIColor = UIColor.clear
128
+ public var pullBarBackgroundColor: UIColor? {
129
+ get { return self.contentViewController.pullBarBackgroundColor }
130
+ set { self.contentViewController.pullBarBackgroundColor = newValue }
131
+ }
132
+
133
+ public static var treatPullBarAsClear: Bool = false
134
+ public var treatPullBarAsClear: Bool {
135
+ get { return self.contentViewController.treatPullBarAsClear }
136
+ set { self.contentViewController.treatPullBarAsClear = newValue }
137
+ }
138
+
139
+ let transition: SheetTransition
140
+
141
+ public var didDismiss: ((SheetViewController) -> Void)?
142
+ public var sizeChanged: ((SheetViewController, SheetSize, CGFloat) -> Void)?
143
+
144
+ public private(set) var contentViewController: SheetContentViewController
145
+ var overlayView = UIView()
146
+ var blurView = UIVisualEffectView()
147
+ var overlayTapView = UIView()
148
+ var overflowView = UIView()
149
+ var overlayTapGesture: UITapGestureRecognizer?
150
+ private var contentViewHeightConstraint: NSLayoutConstraint!
151
+
152
+ /// The child view controller's scroll view we are watching so we can override the pull down/up to work on the sheet when needed
153
+ private weak var childScrollView: UIScrollView?
154
+
155
+ private var keyboardHeight: CGFloat = 0
156
+ private var firstPanPoint: CGPoint = CGPoint.zero
157
+ private var panOffset: CGFloat = 0
158
+ private var panGestureRecognizer: InitialTouchPanGestureRecognizer!
159
+ private var prePanHeight: CGFloat = 0
160
+ private var isPanning: Bool = false
161
+
162
+ public var contentBackgroundColor: UIColor? {
163
+ get { self.contentViewController.contentBackgroundColor }
164
+ set { self.contentViewController.contentBackgroundColor = newValue }
165
+ }
166
+
167
+ public init(controller: UIViewController, sizes: [SheetSize] = [.intrinsic], options: SheetOptions? = nil) {
168
+ let options = options ?? SheetOptions.default
169
+ self.contentViewController = SheetContentViewController(childViewController: controller, options: options)
170
+ if #available(iOS 13.0, *) {
171
+ self.contentViewController.contentBackgroundColor = UIColor.systemBackground
172
+ } else {
173
+ self.contentViewController.contentBackgroundColor = UIColor.white
174
+ }
175
+ self.sizes = sizes.count > 0 ? sizes : [.intrinsic]
176
+ self.options = options
177
+ self.transition = SheetTransition(options: options)
178
+ self.minimumSpaceAbovePullBar = SheetViewController.minimumSpaceAbovePullBar
179
+ super.init(nibName: nil, bundle: nil)
180
+ self.gripColor = SheetViewController.gripColor
181
+ self.gripSize = SheetViewController.gripSize
182
+ self.pullBarBackgroundColor = SheetViewController.pullBarBackgroundColor
183
+ self.cornerRadius = SheetViewController.cornerRadius
184
+ self.updateOrderedSizes()
185
+ self.modalPresentationStyle = .custom
186
+ self.transitioningDelegate = self
187
+ }
188
+
189
+ public required init?(coder: NSCoder) {
190
+ fatalError("init(coder:) has not been implemented")
191
+ }
192
+
193
+ public override func loadView() {
194
+ if self.options.useInlineMode {
195
+ let sheetView = SheetView()
196
+ sheetView.delegate = self
197
+ self.view = sheetView
198
+ } else {
199
+ super.loadView()
200
+ }
201
+ }
202
+
203
+ public override func viewDidLoad() {
204
+ super.viewDidLoad()
205
+
206
+ self.compatibleAdditionalSafeAreaInsets = UIEdgeInsets(top: -self.options.pullBarHeight, left: 0, bottom: 0, right: 0)
207
+
208
+ self.view.backgroundColor = UIColor.clear
209
+ self.addPanGestureRecognizer()
210
+ self.addOverlay()
211
+ self.addBlurBackground()
212
+ self.addContentView()
213
+ self.addOverlayTapView()
214
+ self.registerKeyboardObservers()
215
+ self.resize(to: self.sizes.first ?? .intrinsic, animated: false)
216
+ }
217
+
218
+ public override func viewWillAppear(_ animated: Bool) {
219
+ super.viewWillAppear(animated)
220
+ self.updateOrderedSizes()
221
+ self.contentViewController.updatePreferredHeight()
222
+ self.resize(to: self.currentSize, animated: false)
223
+ }
224
+
225
+ public override func viewWillDisappear(_ animated: Bool) {
226
+ super.viewWillDisappear(animated)
227
+ }
228
+
229
+ public override func viewDidDisappear(_ animated: Bool) {
230
+ super.viewDidDisappear(animated)
231
+ if let presenter = self.transition.presenter, self.options.shrinkPresentingViewController {
232
+ self.transition.restorePresentor(presenter, completion: { _ in
233
+ self.didDismiss?(self)
234
+ })
235
+ } else if !self.options.useInlineMode {
236
+ self.didDismiss?(self)
237
+ }
238
+ }
239
+
240
+ /// Handle a scroll view in the child view controller by watching for the offset for the scrollview and taking priority when at the top (so pulling up/down can grow/shrink the sheet instead of bouncing the child's scroll view)
241
+ public func handleScrollView(_ scrollView: UIScrollView) {
242
+ scrollView.panGestureRecognizer.require(toFail: panGestureRecognizer)
243
+ self.childScrollView = scrollView
244
+ }
245
+
246
+ /// Change the sizes the sheet should try to pin to
247
+ public func setSizes(_ sizes: [SheetSize], animated: Bool = true) {
248
+ guard sizes.count > 0 else {
249
+ return
250
+ }
251
+ self.sizes = sizes
252
+
253
+ self.resize(to: sizes[0], animated: animated)
254
+ }
255
+
256
+ func updateOrderedSizes() {
257
+ var concreteSizes: [(SheetSize, CGFloat)] = self.sizes.map {
258
+ return ($0, self.height(for: $0))
259
+ }
260
+ concreteSizes.sort { $0.1 < $1.1 }
261
+ self.orderedSizes = concreteSizes.map({ size, _ in size })
262
+ }
263
+
264
+ private func addOverlay() {
265
+ self.view.addSubview(self.overlayView)
266
+ Constraints(for: self.overlayView) {
267
+ $0.edges(.top, .left, .right, .bottom).pinToSuperview()
268
+ }
269
+ self.overlayView.isUserInteractionEnabled = false
270
+ self.overlayView.backgroundColor = self.hasBlurBackground ? .clear : self.overlayColor
271
+ }
272
+
273
+ private func addBlurBackground() {
274
+ self.overlayView.addSubview(self.blurView)
275
+ blurView.effect = blurEffect
276
+ Constraints(for: self.blurView) {
277
+ $0.edges(.top, .left, .right, .bottom).pinToSuperview()
278
+ }
279
+ self.blurView.isUserInteractionEnabled = false
280
+ self.blurView.isHidden = !self.hasBlurBackground
281
+ }
282
+
283
+ private func addOverlayTapView() {
284
+ let overlayTapView = self.overlayTapView
285
+ overlayTapView.backgroundColor = .clear
286
+ overlayTapView.isUserInteractionEnabled = !self.allowGestureThroughOverlay
287
+ self.view.addSubview(overlayTapView)
288
+ Constraints(for: overlayTapView, self.contentViewController.view) {
289
+ $0.top.pinToSuperview()
290
+ $0.left.pinToSuperview()
291
+ $0.right.pinToSuperview()
292
+ $0.bottom.align(with: $1.top)
293
+ }
294
+
295
+ let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(overlayTapped))
296
+ self.overlayTapGesture = tapGestureRecognizer
297
+ overlayTapView.addGestureRecognizer(tapGestureRecognizer)
298
+ }
299
+
300
+ @objc func overlayTapped(_ gesture: UITapGestureRecognizer) {
301
+ guard self.dismissOnOverlayTap else { return }
302
+ self.attemptDismiss(animated: true)
303
+ }
304
+
305
+ private func addContentView() {
306
+ self.contentViewController.willMove(toParent: self)
307
+ self.addChild(self.contentViewController)
308
+ self.view.addSubview(self.contentViewController.view)
309
+ self.contentViewController.didMove(toParent: self)
310
+ self.contentViewController.delegate = self
311
+ Constraints(for: self.contentViewController.view) {
312
+ $0.left.pinToSuperview().priority = UILayoutPriority(999)
313
+ $0.left.pinToSuperview(inset: self.options.horizontalPadding, relation: .greaterThanOrEqual)
314
+ if let maxWidth = self.options.maxWidth {
315
+ $0.width.set(maxWidth, relation: .lessThanOrEqual)
316
+ }
317
+
318
+ $0.centerX.alignWithSuperview()
319
+ self.contentViewHeightConstraint = $0.height.set(self.height(for: self.currentSize))
320
+
321
+ let top: CGFloat
322
+ if (self.options.useFullScreenMode) {
323
+ top = 0
324
+ } else {
325
+ top = max(12, UIApplication.shared.windows.first(where: { $0.isKeyWindow })?.compatibleSafeAreaInsets.top ?? 12)
326
+ }
327
+ $0.bottom.pinToSuperview()
328
+ $0.top.pinToSuperview(inset: top, relation: .greaterThanOrEqual).priority = UILayoutPriority(999)
329
+ }
330
+ }
331
+
332
+ private func addPanGestureRecognizer() {
333
+ let panGestureRecognizer = InitialTouchPanGestureRecognizer(target: self, action: #selector(panned(_:)))
334
+ self.view.addGestureRecognizer(panGestureRecognizer)
335
+ panGestureRecognizer.delegate = self
336
+ self.panGestureRecognizer = panGestureRecognizer
337
+ }
338
+
339
+ @objc func panned(_ gesture: UIPanGestureRecognizer) {
340
+ let point = gesture.translation(in: gesture.view?.superview)
341
+ if gesture.state == .began {
342
+ self.firstPanPoint = point
343
+ self.prePanHeight = self.contentViewController.view.bounds.height
344
+ self.isPanning = true
345
+ }
346
+
347
+ let minHeight: CGFloat = self.height(for: self.orderedSizes.first)
348
+ let maxHeight: CGFloat
349
+ if self.allowPullingPastMaxHeight {
350
+ maxHeight = self.height(for: .fullscreen) // self.view.bounds.height
351
+ } else {
352
+ maxHeight = max(self.height(for: self.orderedSizes.last), self.prePanHeight)
353
+ }
354
+
355
+ var newHeight = max(0, self.prePanHeight + (self.firstPanPoint.y - point.y))
356
+ var offset: CGFloat = 0
357
+ if newHeight < minHeight {
358
+ if self.allowPullingPastMinHeight {
359
+ offset = minHeight - newHeight
360
+ }
361
+ newHeight = minHeight
362
+ }
363
+ if newHeight > maxHeight {
364
+ if options.isRubberBandEnabled {
365
+ newHeight = logConstraintValueForYPosition(verticalLimit: maxHeight, yPosition: newHeight)
366
+ } else {
367
+ newHeight = maxHeight
368
+ }
369
+ }
370
+
371
+ switch gesture.state {
372
+ case .cancelled, .failed:
373
+ UIView.animate(withDuration: 0.3, delay: 0, options: [.curveEaseOut], animations: {
374
+ self.contentViewController.view.transform = CGAffineTransform.identity
375
+ self.contentViewHeightConstraint.constant = self.height(for: self.currentSize)
376
+ self.transition.setPresentor(percentComplete: 0)
377
+ self.overlayView.alpha = 1
378
+ }, completion: { _ in
379
+ self.isPanning = false
380
+ })
381
+
382
+ case .began, .changed:
383
+ self.contentViewHeightConstraint.constant = newHeight
384
+
385
+ if offset > 0 {
386
+ let percent = max(0, min(1, offset / max(1, newHeight)))
387
+ self.transition.setPresentor(percentComplete: percent)
388
+ self.overlayView.alpha = 1 - percent
389
+ self.contentViewController.view.transform = CGAffineTransform(translationX: 0, y: offset)
390
+ } else {
391
+ self.contentViewController.view.transform = CGAffineTransform.identity
392
+ }
393
+ case .ended:
394
+ let velocity = (0.2 * gesture.velocity(in: self.view).y)
395
+ var finalHeight = newHeight - offset - velocity
396
+ if velocity > options.pullDismissThreshod {
397
+ // They swiped hard, always just close the sheet when they do
398
+ finalHeight = -1
399
+ }
400
+
401
+ let animationDuration = TimeInterval(abs(velocity*0.0002) + 0.2)
402
+
403
+ if (finalHeight <= 0 || finalHeight < (self.height(for: currentSize) / 2)) && dismissOnPull {
404
+ // Dismiss
405
+ UIView.animate(
406
+ withDuration: animationDuration,
407
+ delay: 0,
408
+ usingSpringWithDamping: self.options.transitionDampening,
409
+ initialSpringVelocity: self.options.transitionVelocity,
410
+ options: self.options.transitionAnimationOptions,
411
+ animations: {
412
+ self.contentViewController.view.transform = CGAffineTransform(translationX: 0, y: self.contentViewController.view.bounds.height)
413
+ self.view.backgroundColor = UIColor.clear
414
+ self.transition.setPresentor(percentComplete: 1)
415
+ self.overlayView.alpha = 0
416
+ }, completion: { complete in
417
+ self.attemptDismiss(animated: false)
418
+ })
419
+ return
420
+ }
421
+
422
+
423
+ var newSize = self.currentSize
424
+ if point.y < 0 {
425
+ // We need to move to the next larger one
426
+ newSize = self.orderedSizes.last ?? self.currentSize
427
+ for size in self.orderedSizes.reversed() {
428
+ if finalHeight < self.height(for: size) {
429
+ newSize = size
430
+ } else {
431
+ break
432
+ }
433
+ }
434
+ } else {
435
+ // We need to move to the next smaller one
436
+ newSize = self.orderedSizes.first ?? self.currentSize
437
+ for size in self.orderedSizes {
438
+ if finalHeight > self.height(for: size) {
439
+ newSize = size
440
+ } else {
441
+ break
442
+ }
443
+ }
444
+ }
445
+ let previousSize = self.currentSize
446
+ self.currentSize = newSize
447
+
448
+ let newContentHeight = self.height(for: newSize)
449
+ UIView.animate(
450
+ withDuration: animationDuration,
451
+ delay: 0,
452
+ usingSpringWithDamping: self.options.transitionDampening,
453
+ initialSpringVelocity: self.options.transitionVelocity,
454
+ options: self.options.transitionAnimationOptions,
455
+ animations: {
456
+ self.contentViewController.view.transform = CGAffineTransform.identity
457
+ self.contentViewHeightConstraint.constant = newContentHeight
458
+ self.transition.setPresentor(percentComplete: 0)
459
+ self.overlayView.alpha = 1
460
+ self.view.layoutIfNeeded()
461
+ }, completion: { complete in
462
+ self.isPanning = false
463
+ if previousSize != newSize {
464
+ self.sizeChanged?(self, newSize, newContentHeight)
465
+ }
466
+ })
467
+ case .possible:
468
+ break
469
+ @unknown default:
470
+ break // Do nothing
471
+ }
472
+ }
473
+
474
+ private func registerKeyboardObservers() {
475
+ NotificationCenter.default.addObserver(self, selector: #selector(keyboardShown(_:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
476
+ NotificationCenter.default.addObserver(self, selector: #selector(keyboardDismissed(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
477
+ }
478
+
479
+ @objc func keyboardShown(_ notification: Notification) {
480
+ guard let info:[AnyHashable: Any] = notification.userInfo, let keyboardRect:CGRect = (info[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return }
481
+
482
+ let windowRect = self.view.convert(self.view.bounds, to: nil)
483
+ let actualHeight = windowRect.maxY - keyboardRect.origin.y
484
+ self.adjustForKeyboard(height: actualHeight, from: notification)
485
+ }
486
+
487
+ @objc func keyboardDismissed(_ notification: Notification) {
488
+ self.adjustForKeyboard(height: 0, from: notification)
489
+ }
490
+
491
+ private func adjustForKeyboard(height: CGFloat, from notification: Notification) {
492
+ guard self.autoAdjustToKeyboard, let info:[AnyHashable: Any] = notification.userInfo else { return }
493
+ self.keyboardHeight = height
494
+
495
+ let duration:TimeInterval = (info[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
496
+ let animationCurveRawNSN = info[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber
497
+ let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIView.AnimationOptions.curveEaseInOut.rawValue
498
+ let animationCurve:UIView.AnimationOptions = UIView.AnimationOptions(rawValue: animationCurveRaw)
499
+
500
+ self.contentViewController.adjustForKeyboard(height: self.keyboardHeight)
501
+ self.resize(to: self.currentSize, duration: duration, options: animationCurve, animated: true, complete: {
502
+ self.resize(to: self.currentSize)
503
+ })
504
+ }
505
+
506
+ private func height(for size: SheetSize?) -> CGFloat {
507
+ guard let size = size else { return 0 }
508
+ let contentHeight: CGFloat
509
+ let fullscreenHeight: CGFloat
510
+ if self.options.useFullScreenMode {
511
+ fullscreenHeight = self.view.bounds.height - self.minimumSpaceAbovePullBar
512
+ } else {
513
+ fullscreenHeight = self.view.bounds.height - self.view.compatibleSafeAreaInsets.top - self.minimumSpaceAbovePullBar
514
+ }
515
+ switch (size) {
516
+ case .fixed(let height):
517
+ contentHeight = height + self.keyboardHeight
518
+ case .fullscreen:
519
+ contentHeight = fullscreenHeight
520
+ case .intrinsic:
521
+ contentHeight = self.contentViewController.preferredHeight + self.keyboardHeight
522
+ case .percent(let percent):
523
+ if (percent > 1) {
524
+ debugPrint("Size percent should be less than or equal to 1.0, but was set to \(percent))")
525
+ }
526
+ contentHeight = (self.view.bounds.height) * CGFloat(percent) + self.keyboardHeight
527
+ case .marginFromTop(let margin):
528
+ contentHeight = (self.view.bounds.height) - margin + self.keyboardHeight
529
+ }
530
+ return min(fullscreenHeight, contentHeight)
531
+ }
532
+
533
+ // https://medium.com/thoughts-on-thoughts/recreating-apple-s-rubber-band-effect-in-swift-dbf981b40f35
534
+ private func logConstraintValueForYPosition(verticalLimit: CGFloat, yPosition : CGFloat) -> CGFloat {
535
+ return verticalLimit * (1 + log10(yPosition/verticalLimit))
536
+ }
537
+
538
+ public func resize(to size: SheetSize,
539
+ duration: TimeInterval = 0.2,
540
+ options: UIView.AnimationOptions = [.curveEaseOut],
541
+ animated: Bool = true,
542
+ complete: (() -> Void)? = nil) {
543
+
544
+ let previousSize = self.currentSize
545
+ self.currentSize = size
546
+
547
+ let oldConstraintHeight = self.contentViewHeightConstraint.constant
548
+
549
+ let newHeight = self.height(for: size)
550
+
551
+ guard oldConstraintHeight != newHeight else {
552
+ return
553
+ }
554
+
555
+ if animated {
556
+ UIView.animate(withDuration: duration, delay: 0, options: options, animations: { [weak self] in
557
+ guard let self = self, let constraint = self.contentViewHeightConstraint else { return }
558
+ constraint.constant = newHeight
559
+ self.view.layoutIfNeeded()
560
+ }, completion: { _ in
561
+ if previousSize != size {
562
+ self.sizeChanged?(self, size, newHeight)
563
+ }
564
+ self.contentViewController.updateAfterLayout()
565
+ complete?()
566
+ })
567
+ } else {
568
+ UIView.performWithoutAnimation {
569
+ self.contentViewHeightConstraint?.constant = self.height(for: size)
570
+ self.contentViewController.view.layoutIfNeeded()
571
+ }
572
+ complete?()
573
+ }
574
+ }
575
+
576
+ public func attemptDismiss(animated: Bool) {
577
+ if self.options.useInlineMode {
578
+ if animated {
579
+ self.animateOut {
580
+ self.didDismiss?(self)
581
+ }
582
+ } else {
583
+ self.view.removeFromSuperview()
584
+ self.removeFromParent()
585
+ self.didDismiss?(self)
586
+ }
587
+ } else {
588
+ self.dismiss(animated: animated, completion: nil)
589
+ }
590
+ }
591
+
592
+ /// Recalculates the intrinsic height of the sheet based on the content, and updates the sheet height to match.
593
+ ///
594
+ /// **Note:** Only meant for use with `.intrinsic` sheet size
595
+ public func updateIntrinsicHeight() {
596
+ contentViewController.updatePreferredHeight()
597
+ }
598
+
599
+ /// Animates the sheet in, but only if presenting using the inline mode
600
+ public func animateIn(to view: UIView, in parent: UIViewController, size: SheetSize? = nil, duration: TimeInterval = 0.3, completion: (() -> Void)? = nil) {
601
+
602
+ self.willMove(toParent: parent)
603
+ parent.addChild(self)
604
+ view.addSubview(self.view)
605
+ self.didMove(toParent: parent)
606
+
607
+ self.view.translatesAutoresizingMaskIntoConstraints = false
608
+ NSLayoutConstraint.activate([
609
+ self.view.topAnchor.constraint(equalTo: view.topAnchor),
610
+ self.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
611
+ self.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
612
+ self.view.trailingAnchor.constraint(equalTo: view.trailingAnchor)
613
+ ])
614
+ self.animateIn(size: size, duration: duration, completion: completion)
615
+ }
616
+
617
+ public func animateIn(size: SheetSize? = nil, duration: TimeInterval = 0.3, completion: (() -> Void)? = nil) {
618
+ guard self.options.useInlineMode else { return }
619
+ guard self.view.superview != nil else {
620
+ print("It appears your sheet is not set as a subview of another view. Make sure to add this view as a subview before trying to animate it in.")
621
+ return
622
+ }
623
+ self.view.superview?.layoutIfNeeded()
624
+ self.contentViewController.updatePreferredHeight()
625
+ self.resize(to: size ?? self.sizes.first ?? self.currentSize, animated: false)
626
+ let contentView = self.contentViewController.view!
627
+ contentView.transform = CGAffineTransform(translationX: 0, y: contentView.bounds.height)
628
+ self.overlayView.alpha = 0
629
+ self.updateOrderedSizes()
630
+
631
+ UIView.animate(
632
+ withDuration: duration,
633
+ animations: {
634
+ contentView.transform = .identity
635
+ self.overlayView.alpha = 1
636
+ },
637
+ completion: { _ in
638
+ completion?()
639
+ }
640
+ )
641
+ }
642
+
643
+ /// Animates the sheet out, but only if presenting using the inline mode
644
+ public func animateOut(duration: TimeInterval = 0.3, completion: (() -> Void)? = nil) {
645
+ guard self.options.useInlineMode else { return }
646
+ let contentView = self.contentViewController.view!
647
+
648
+ UIView.animate(
649
+ withDuration: duration,
650
+ delay: 0,
651
+ usingSpringWithDamping: self.options.transitionDampening,
652
+ initialSpringVelocity: self.options.transitionVelocity,
653
+ options: self.options.transitionAnimationOptions,
654
+ animations: {
655
+ contentView.transform = CGAffineTransform(translationX: 0, y: contentView.bounds.height)
656
+ self.overlayView.alpha = 0
657
+ },
658
+ completion: { _ in
659
+ self.view.removeFromSuperview()
660
+ self.removeFromParent()
661
+ completion?()
662
+ }
663
+ )
664
+ }
665
+ }
666
+
667
+ extension SheetViewController: SheetViewDelegate {
668
+ func sheetPoint(inside point: CGPoint, with event: UIEvent?) -> Bool {
669
+ let isInOverlay = self.overlayTapView.bounds.contains(point)
670
+ if self.allowGestureThroughOverlay, isInOverlay {
671
+ return false
672
+ } else {
673
+ return true
674
+ }
675
+ }
676
+ }
677
+
678
+ extension SheetViewController: UIGestureRecognizerDelegate {
679
+ public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
680
+ // Allowing gesture recognition on a UIControl seems to prevent its events from firing properly sometimes
681
+ if !shouldRecognizePanGestureWithUIControls {
682
+ if let view = touch.view {
683
+ return !(view is UIControl)
684
+ }
685
+ }
686
+ return true
687
+ }
688
+
689
+ public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
690
+ guard let panGestureRecognizer = gestureRecognizer as? InitialTouchPanGestureRecognizer else { return true }
691
+ guard let childScrollView = self.childScrollView else { return true }
692
+ guard let point = panGestureRecognizer.initialTouchLocation else { return true }
693
+
694
+ let pointInChildScrollView = self.view.convert(point, to: childScrollView).y - childScrollView.contentOffset.y
695
+
696
+ let velocity = panGestureRecognizer.velocity(in: panGestureRecognizer.view?.superview)
697
+ guard pointInChildScrollView > 0, pointInChildScrollView < childScrollView.bounds.height else {
698
+ if keyboardHeight > 0 {
699
+ childScrollView.endEditing(true)
700
+ }
701
+ return true
702
+ }
703
+ let topInset = childScrollView.contentInset.top
704
+
705
+ if !(abs(velocity.y) > abs(velocity.x) && childScrollView.contentOffset.y <= -topInset) {
706
+ return false
707
+ }
708
+
709
+ if velocity.y < 0 {
710
+ let containerHeight = height(for: self.currentSize)
711
+ return height(for: self.orderedSizes.last) > containerHeight && containerHeight < height(for: SheetSize.fullscreen)
712
+ } else {
713
+ return true
714
+ }
715
+ }
716
+ }
717
+
718
+ extension SheetViewController: SheetContentViewDelegate {
719
+ func pullBarTapped() {
720
+ // Tapping the pull bar is just for accessibility
721
+ guard UIAccessibility.isVoiceOverRunning else { return }
722
+ let shouldDismiss = self.allowGestureThroughOverlay && (self.dismissOnOverlayTap || self.dismissOnPull)
723
+ guard !shouldDismiss else {
724
+ self.attemptDismiss(animated: true)
725
+ return
726
+ }
727
+
728
+ if self.sizes.count > 1 {
729
+ let index = (self.sizes.firstIndex(of: self.currentSize) ?? 0) + 1
730
+ if index >= self.sizes.count {
731
+ self.resize(to: self.sizes[0])
732
+ } else {
733
+ self.resize(to: self.sizes[index])
734
+ }
735
+ }
736
+ }
737
+
738
+ func preferredHeightChanged(oldHeight: CGFloat, newSize: CGFloat) {
739
+ if self.sizes.contains(.intrinsic) {
740
+ self.updateOrderedSizes()
741
+ }
742
+ // If our intrinsic size changed and that is what we are sized to currently, use that
743
+ if self.currentSize == .intrinsic, !self.isPanning {
744
+ self.resize(to: .intrinsic)
745
+ }
746
+ }
747
+ }
748
+
749
+ extension SheetViewController: UIViewControllerTransitioningDelegate {
750
+ public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
751
+ transition.presenting = true
752
+ return transition
753
+ }
754
+
755
+ public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
756
+ transition.presenting = false
757
+ return transition
758
+ }
759
+ }
760
+
761
+ #endif // os(iOS) || os(tvOS) || os(watchOS)