-
-
Notifications
You must be signed in to change notification settings - Fork 697
Expand file tree
/
Copy pathSideMenuNavigationController.swift
More file actions
658 lines (552 loc) · 26.7 KB
/
SideMenuNavigationController.swift
File metadata and controls
658 lines (552 loc) · 26.7 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
//
// SideMenuNavigationController.swift
//
// Created by Jon Kent on 1/14/16.
// Copyright © 2016 Jon Kent. All rights reserved.
//
import UIKit
@objc public enum SideMenuPushStyle: Int { case
`default`,
popWhenPossible,
preserve,
preserveAndHideBackButton,
replace,
subMenu
internal var hidesBackButton: Bool {
switch self {
case .preserveAndHideBackButton, .replace: return true
case .default, .popWhenPossible, .preserve, .subMenu: return false
}
}
}
internal protocol MenuModel {
/// Prevents the same view controller (or a view controller of the same class) from being pushed more than once. Defaults to true.
var allowPushOfSameClassTwice: Bool { get }
/// Forces menus to always animate when appearing or disappearing, regardless of a pushed view controller's animation.
var alwaysAnimate: Bool { get }
/**
The blur effect style of the menu if the menu's root view controller is a UITableViewController or UICollectionViewController.
- Note: If you want cells in a UITableViewController menu to show vibrancy, make them a subclass of UITableViewVibrantCell.
*/
var blurEffectStyle: UIBlurEffect.Style? { get }
/// Animation curve of the remaining animation when the menu is partially dismissed with gestures. Default is .easeIn.
var completionCurve: UIView.AnimationCurve { get }
/// Automatically dismisses the menu when another view is presented from it.
var dismissOnPresent: Bool { get }
/// Automatically dismisses the menu when another view controller is pushed from it.
var dismissOnPush: Bool { get }
/// Automatically dismisses the menu when the screen is rotated.
var dismissOnRotation: Bool { get }
/// Automatically dismisses the menu when app goes to the background.
var dismissWhenBackgrounded: Bool { get }
/// Enable or disable a swipe gesture that dismisses the menu. Will not be triggered when `presentingViewControllerUserInteractionEnabled` is set to true. Default is true.
var enableSwipeToDismissGesture: Bool { get }
/// Enable or disable a tap gesture that dismisses the menu. Will not be triggered when `presentingViewControllerUserInteractionEnabled` is set to true. Default is true.
var enableTapToDismissGesture: Bool { get }
/**
The push style of the menu.
There are six modes in MenuPushStyle:
- defaultBehavior: The view controller is pushed onto the stack.
- popWhenPossible: If a view controller already in the stack is of the same class as the pushed view controller, the stack is instead popped back to the existing view controller. This behavior can help users from getting lost in a deep navigation stack.
- preserve: If a view controller already in the stack is of the same class as the pushed view controller, the existing view controller is pushed to the end of the stack. This behavior is similar to a UITabBarController.
- preserveAndHideBackButton: Same as .preserve and back buttons are automatically hidden.
- replace: Any existing view controllers are released from the stack and replaced with the pushed view controller. Back buttons are automatically hidden. This behavior is ideal if view controllers require a lot of memory or their state doesn't need to be preserved..
- subMenu: Unlike all other behaviors that push using the menu's presentingViewController, this behavior pushes view controllers within the menu. Use this behavior if you want to display a sub menu.
*/
var pushStyle: SideMenuPushStyle { get }
}
@objc public protocol SideMenuNavigationControllerDelegate {
@objc optional func sideMenuWillAppear(menu: SideMenuNavigationController, animated: Bool)
@objc optional func sideMenuDidAppear(menu: SideMenuNavigationController, animated: Bool)
@objc optional func sideMenuWillDisappear(menu: SideMenuNavigationController, animated: Bool)
@objc optional func sideMenuDidDisappear(menu: SideMenuNavigationController, animated: Bool)
}
internal protocol SideMenuNavigationControllerTransitionDelegate: class {
func sideMenuTransitionDidDismiss(menu: Menu)
}
public struct SideMenuSettings: Model, InitializableStruct {
public var allowPushOfSameClassTwice: Bool = true
public var alwaysAnimate: Bool = true
public var animationOptions: UIView.AnimationOptions = .curveEaseInOut
public var blurEffectStyle: UIBlurEffect.Style? = nil
public var completeGestureDuration: Double = 0.35
public var completionCurve: UIView.AnimationCurve = .easeIn
public var dismissDuration: Double = 0.35
public var dismissOnPresent: Bool = true
public var dismissOnPush: Bool = true
public var dismissOnRotation: Bool = true
public var dismissWhenBackgrounded: Bool = true
public var enableSwipeToDismissGesture: Bool = true
public var enableTapToDismissGesture: Bool = true
public var initialSpringVelocity: CGFloat = 1
public var menuWidth: CGFloat = {
let appScreenRect = UIApplication.shared.keyWindow?.bounds ?? UIWindow().bounds
let minimumSize = min(appScreenRect.width, appScreenRect.height)
return min(round(minimumSize * 0.75), 240)
}()
public var presentingViewControllerUserInteractionEnabled: Bool = false
public var presentingViewControllerUseSnapshot: Bool = false
public var presentDuration: Double = 0.35
public var presentationStyle: SideMenuPresentationStyle = .viewSlideOut
public var pushStyle: SideMenuPushStyle = .default
public var statusBarEndAlpha: CGFloat = 0
public var usingSpringWithDamping: CGFloat = 1
public init() {}
}
internal typealias Menu = SideMenuNavigationController
typealias Model = MenuModel & PresentationModel & AnimationModel
@objcMembers
open class SideMenuNavigationController: UINavigationController {
private lazy var _leftSide = Protected(false) { [weak self] oldValue, newValue in
guard self?.isHidden != false else {
Print.warning(.property, arguments: .leftSide, required: true)
return oldValue
}
return newValue
}
private weak var _sideMenuManager: SideMenuManager?
private weak var foundViewController: UIViewController?
private var originalBackgroundColor: UIColor?
private var rotating: Bool = false
private var transitionController: SideMenuTransitionController?
private var transitionInteractive: Bool = false
/// Delegate for receiving appear and disappear related events. If `nil` the visible view controller that displays a `SideMenuNavigationController` automatically receives these events.
public weak var sideMenuDelegate: SideMenuNavigationControllerDelegate?
/// The swipe to dismiss gesture.
open private(set) weak var swipeToDismissGesture: UIPanGestureRecognizer? = nil
/// The tap to dismiss gesture.
open private(set) weak var tapToDismissGesture: UITapGestureRecognizer? = nil
open var sideMenuManager: SideMenuManager {
get { return _sideMenuManager ?? SideMenuManager.default }
set {
newValue.setMenu(self, forLeftSide: leftSide)
if let sideMenuManager = _sideMenuManager, sideMenuManager !== newValue {
let side = SideMenuManager.PresentDirection(leftSide: leftSide)
Print.warning(.menuAlreadyAssigned, arguments: String(describing: self.self), side.name, String(describing: newValue))
}
_sideMenuManager = newValue
}
}
/// The menu settings.
open var settings = SideMenuSettings() {
didSet {
setupBlur()
if !enableSwipeToDismissGesture {
swipeToDismissGesture?.remove()
}
if !enableTapToDismissGesture {
tapToDismissGesture?.remove()
}
}
}
public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
setup()
}
public init(rootViewController: UIViewController, settings: SideMenuSettings = SideMenuSettings()) {
self.settings = settings
super.init(rootViewController: rootViewController)
setup()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
override open func awakeFromNib() {
super.awakeFromNib()
sideMenuManager.setMenu(self, forLeftSide: leftSide)
}
override open func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if topViewController == nil {
Print.warning(.emptyMenu)
}
// Dismiss keyboard to prevent weird keyboard animations from occurring during transition
presentingViewController?.view.endEditing(true)
foundViewController = nil
activeDelegate?.sideMenuWillAppear?(menu: self, animated: animated)
}
override open func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// We had presented a view before, so lets dismiss ourselves as already acted upon
if view.isHidden {
dismiss(animated: false, completion: { [weak self] in
self?.view.isHidden = false
})
} else {
activeDelegate?.sideMenuDidAppear?(menu: self, animated: animated)
}
}
override open func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
defer { activeDelegate?.sideMenuWillDisappear?(menu: self, animated: animated) }
guard !isBeingDismissed else { return }
// When presenting a view controller from the menu, the menu view gets moved into another transition view above our transition container
// which can break the visual layout we had before. So, we move the menu view back to its original transition view to preserve it.
if let presentingView = presentingViewController?.view, let containerView = presentingView.superview {
containerView.addSubview(view)
}
if dismissOnPresent {
// We're presenting a view controller from the menu, so we need to hide the menu so it isn't showing when the presented view is dismissed.
transitionController?.transition(presenting: false, animated: animated, alongsideTransition: { [weak self] in
guard let self = self else { return }
self.activeDelegate?.sideMenuWillDisappear?(menu: self, animated: animated)
}, complete: false, completion: { [weak self] _ in
guard let self = self else { return }
self.activeDelegate?.sideMenuDidDisappear?(menu: self, animated: animated)
self.view.isHidden = true
})
}
}
override open func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
// Work-around: if the menu is dismissed without animation the transition logic is never called to restore the
// the view hierarchy leaving the screen black/empty. This is because the transition moves views within a container
// view, but dismissing without animation removes the container view before the original hierarchy is restored.
// This check corrects that.
if isBeingDismissed {
transitionController?.transition(presenting: false, animated: false)
}
// Clear selection on UITableViewControllers when reappearing using custom transitions
if let tableViewController = topViewController as? UITableViewController,
let tableView = tableViewController.tableView,
let indexPaths = tableView.indexPathsForSelectedRows,
tableViewController.clearsSelectionOnViewWillAppear {
indexPaths.forEach { tableView.deselectRow(at: $0, animated: false) }
}
activeDelegate?.sideMenuDidDisappear?(menu: self, animated: animated)
if isBeingDismissed {
transitionController = nil
} else if dismissOnPresent {
view.isHidden = true
}
}
override open func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
// Don't bother resizing if the view isn't visible
guard let transitionController = transitionController, !view.isHidden,
UIApplication.shared.applicationState != UIApplication.State.background,
UIApplication.shared.applicationState != UIApplication.State.inactive else { return }
rotating = true
let dismiss = self.presentingViewControllerUseSnapshot || self.dismissOnRotation
coordinator.animate(alongsideTransition: { _ in
if dismiss {
transitionController.transition(presenting: false, animated: false, complete: false)
} else {
transitionController.layout()
}
}) { [weak self] _ in
guard let self = self else { return }
if dismiss {
self.dismissMenu(animated: false)
}
self.rotating = false
}
}
open override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
transitionController?.layout()
}
override open func pushViewController(_ viewController: UIViewController, animated: Bool) {
guard viewControllers.count > 0 else {
// NOTE: pushViewController is called by init(rootViewController: UIViewController)
// so we must perform the normal super method in this case
return super.pushViewController(viewController, animated: animated)
}
var alongsideTransition: (() -> Void)? = nil
if dismissOnPush {
alongsideTransition = { [weak self] in
guard let self = self else { return }
self.dismissAnimation(animated: animated || self.alwaysAnimate)
}
}
let pushed = SideMenuPushCoordinator(config:
.init(
allowPushOfSameClassTwice: allowPushOfSameClassTwice,
alongsideTransition: alongsideTransition,
animated: animated,
fromViewController: self,
pushStyle: pushStyle,
toViewController: viewController
)
).start()
if !pushed {
super.pushViewController(viewController, animated: animated)
}
}
override open var transitioningDelegate: UIViewControllerTransitioningDelegate? {
get {
guard transitionController == nil else { return transitionController }
transitionController = SideMenuTransitionController(leftSide: leftSide, config: settings)
transitionController?.delegate = self
transitionController?.interactive = transitionInteractive
transitionInteractive = false
return transitionController
}
set { Print.warning(.transitioningDelegate, required: true) }
}
}
// Interface
extension SideMenuNavigationController: Model {
@IBInspectable open var allowPushOfSameClassTwice: Bool {
get { return settings.allowPushOfSameClassTwice }
set { settings.allowPushOfSameClassTwice = newValue }
}
@IBInspectable open var alwaysAnimate: Bool {
get { return settings.alwaysAnimate }
set { settings.alwaysAnimate = newValue }
}
@IBInspectable open var animationOptions: UIView.AnimationOptions {
get { return settings.animationOptions }
set { settings.animationOptions = newValue }
}
open var blurEffectStyle: UIBlurEffect.Style? {
get { return settings.blurEffectStyle }
set { settings.blurEffectStyle = newValue }
}
@IBInspectable open var completeGestureDuration: Double {
get { return settings.completeGestureDuration }
set { settings.completeGestureDuration = newValue }
}
@IBInspectable open var completionCurve: UIView.AnimationCurve {
get { return settings.completionCurve }
set { settings.completionCurve = newValue }
}
@IBInspectable open var dismissDuration: Double {
get { return settings.dismissDuration }
set { settings.dismissDuration = newValue }
}
@IBInspectable open var dismissOnPresent: Bool {
get { return settings.dismissOnPresent }
set { settings.dismissOnPresent = newValue }
}
@IBInspectable open var dismissOnPush: Bool {
get { return settings.dismissOnPush }
set { settings.dismissOnPush = newValue }
}
@IBInspectable open var dismissOnRotation: Bool {
get { return settings.dismissOnRotation }
set { settings.dismissOnRotation = newValue }
}
@IBInspectable open var dismissWhenBackgrounded: Bool {
get { return settings.dismissWhenBackgrounded }
set { settings.dismissWhenBackgrounded = newValue }
}
@IBInspectable open var enableSwipeToDismissGesture: Bool {
get { return settings.enableSwipeToDismissGesture }
set { settings.enableSwipeToDismissGesture = newValue }
}
@IBInspectable open var enableTapToDismissGesture: Bool {
get { return settings.enableTapToDismissGesture }
set { settings.enableTapToDismissGesture = newValue }
}
@IBInspectable open var initialSpringVelocity: CGFloat {
get { return settings.initialSpringVelocity }
set { settings.initialSpringVelocity = newValue }
}
/// Whether the menu appears on the right or left side of the screen. Right is the default. This property cannot be changed after the menu has loaded.
@IBInspectable open var leftSide: Bool {
get { return _leftSide.value }
set { _leftSide.value = newValue }
}
/// Indicates if the menu is anywhere in the view hierarchy, even if covered by another view controller.
open override var isHidden: Bool {
return super.isHidden
}
@IBInspectable open var menuWidth: CGFloat {
get { return settings.menuWidth }
set { settings.menuWidth = newValue }
}
@IBInspectable open var presentingViewControllerUserInteractionEnabled: Bool {
get { return settings.presentingViewControllerUserInteractionEnabled }
set { settings.presentingViewControllerUserInteractionEnabled = newValue }
}
@IBInspectable open var presentingViewControllerUseSnapshot: Bool {
get { return settings.presentingViewControllerUseSnapshot }
set { settings.presentingViewControllerUseSnapshot = newValue }
}
@IBInspectable open var presentDuration: Double {
get { return settings.presentDuration }
set { settings.presentDuration = newValue }
}
open var presentationStyle: SideMenuPresentationStyle {
get { return settings.presentationStyle }
set { settings.presentationStyle = newValue }
}
@IBInspectable open var pushStyle: SideMenuPushStyle {
get { return settings.pushStyle }
set { settings.pushStyle = newValue }
}
@IBInspectable open var statusBarEndAlpha: CGFloat {
get { return settings.statusBarEndAlpha }
set { settings.statusBarEndAlpha = newValue }
}
@IBInspectable open var usingSpringWithDamping: CGFloat {
get { return settings.usingSpringWithDamping }
set { settings.usingSpringWithDamping = newValue }
}
}
extension SideMenuNavigationController: SideMenuTransitionControllerDelegate {
func sideMenuTransitionController(_ transitionController: SideMenuTransitionController, didDismiss viewController: UIViewController) {
sideMenuManager.sideMenuTransitionDidDismiss(menu: self)
}
func sideMenuTransitionController(_ transitionController: SideMenuTransitionController, didPresent viewController: UIViewController) {
swipeToDismissGesture?.remove()
swipeToDismissGesture = addSwipeToDismissGesture(to: view.superview)
tapToDismissGesture = addTapToDismissGesture(to: view.superview)
}
}
internal extension SideMenuNavigationController {
func handleMenuPan(_ gesture: UIPanGestureRecognizer, _ presenting: Bool) {
let width = menuWidth
let distance = gesture.xTranslation / width
let progress = max(min(distance * factor(presenting), 1), 0)
switch (gesture.state) {
case .began:
if !presenting {
dismissMenu(interactively: true)
}
fallthrough
case .changed:
transitionController?.handle(state: .update(progress: progress))
case .ended:
let velocity = gesture.xVelocity * factor(presenting)
let finished = velocity >= 100 || velocity >= -50 && abs(progress) >= 0.5
transitionController?.handle(state: finished ? .finish : .cancel)
default:
transitionController?.handle(state: .cancel)
}
}
func cancelMenuPan(_ gesture: UIPanGestureRecognizer) {
transitionController?.handle(state: .cancel)
}
func dismissMenu(animated flag: Bool = true, interactively: Bool = false, completion: (() -> Void)? = nil) {
guard !isHidden else { return }
transitionController?.interactive = interactively
dismiss(animated: flag, completion: completion)
}
// Note: although this method is syntactically reversed it allows the interactive property to scoped privately
func present(from viewController: UIViewController?, interactively: Bool, completion: (() -> Void)? = nil) {
guard let viewController = viewController else { return }
transitionInteractive = interactively
viewController.present(self, animated: true, completion: completion)
}
}
private extension SideMenuNavigationController {
weak var activeDelegate: SideMenuNavigationControllerDelegate? {
guard !view.isHidden else { return nil }
if let sideMenuDelegate = sideMenuDelegate { return sideMenuDelegate }
return findViewController as? SideMenuNavigationControllerDelegate
}
var findViewController: UIViewController? {
foundViewController = foundViewController ?? presentingViewController?.activeViewController
return foundViewController
}
func dismissAnimation(animated: Bool) {
transitionController?.transition(presenting: false, animated: animated, alongsideTransition: { [weak self] in
guard let self = self else { return }
self.activeDelegate?.sideMenuWillDisappear?(menu: self, animated: animated)
}, completion: { [weak self] _ in
guard let self = self else { return }
self.activeDelegate?.sideMenuDidDisappear?(menu: self, animated: animated)
self.dismiss(animated: false, completion: nil)
self.foundViewController = nil
})
}
func setup() {
modalPresentationStyle = .overFullScreen
setupBlur()
if #available(iOS 13.0, *) {} else {
registerForNotifications()
}
}
func setupBlur() {
removeBlur()
guard let blurEffectStyle = blurEffectStyle,
let view = topViewController?.view,
!UIAccessibility.isReduceTransparencyEnabled else {
return
}
originalBackgroundColor = originalBackgroundColor ?? view.backgroundColor
let blurEffect = UIBlurEffect(style: blurEffectStyle)
let blurView = UIVisualEffectView(effect: blurEffect)
view.backgroundColor = UIColor.clear
if let tableViewController = topViewController as? UITableViewController {
tableViewController.tableView.backgroundView = blurView
tableViewController.tableView.separatorEffect = UIVibrancyEffect(blurEffect: blurEffect)
tableViewController.tableView.reloadData()
} else {
blurView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
blurView.frame = view.bounds
view.insertSubview(blurView, at: 0)
}
}
func removeBlur() {
guard let originalBackgroundColor = originalBackgroundColor,
let view = topViewController?.view else {
return
}
self.originalBackgroundColor = nil
view.backgroundColor = originalBackgroundColor
if let tableViewController = topViewController as? UITableViewController {
tableViewController.tableView.backgroundView = nil
tableViewController.tableView.separatorEffect = nil
tableViewController.tableView.reloadData()
} else if let blurView = view.subviews.first as? UIVisualEffectView {
blurView.removeFromSuperview()
}
}
@available(iOS, deprecated: 13.0)
func registerForNotifications() {
NotificationCenter.default.removeObserver(self)
[UIApplication.willChangeStatusBarFrameNotification,
UIApplication.didEnterBackgroundNotification].forEach {
NotificationCenter.default.addObserver(self, selector: #selector(handleNotification), name: $0, object: nil)
}
}
@available(iOS, deprecated: 13.0)
@objc func handleNotification(notification: NSNotification) {
guard isHidden else { return }
switch notification.name {
case UIApplication.willChangeStatusBarFrameNotification:
// Dismiss for in-call status bar changes but not rotation
if !rotating {
dismissMenu()
}
case UIApplication.didEnterBackgroundNotification:
if dismissWhenBackgrounded {
dismissMenu()
}
default: break
}
}
@discardableResult func addSwipeToDismissGesture(to view: UIView?) -> UIPanGestureRecognizer? {
guard enableSwipeToDismissGesture else { return nil }
return UIPanGestureRecognizer(addTo: view, target: self, action: #selector(handleDismissMenuPan(_:)))?.with {
$0.cancelsTouchesInView = false
}
}
@discardableResult func addTapToDismissGesture(to view: UIView?) -> UITapGestureRecognizer? {
guard enableTapToDismissGesture else { return nil }
return UITapGestureRecognizer(addTo: view, target: self, action: #selector(handleDismissMenuTap(_:)))?.with {
$0.cancelsTouchesInView = false
}
}
@objc func handleDismissMenuTap(_ tap: UITapGestureRecognizer) {
let hitTest = view.window?.hitTest(tap.location(in: view.superview), with: nil)
guard hitTest == view.superview else { return }
dismissMenu()
}
@objc func handleDismissMenuPan(_ gesture: UIPanGestureRecognizer) {
handleMenuPan(gesture, false)
}
func factor(_ presenting: Bool) -> CGFloat {
return presenting ? presentFactor : hideFactor
}
var presentFactor: CGFloat {
return leftSide ? 1 : -1
}
var hideFactor: CGFloat {
return -presentFactor
}
}