-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathAMSlideMenuMainViewController.swift
More file actions
568 lines (492 loc) · 20.3 KB
/
AMSlideMenuMainViewController.swift
File metadata and controls
568 lines (492 loc) · 20.3 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
//
// AMSlideMenuMainViewController.swift
// AMSlideMenu
//
// The MIT License (MIT)
//
// Created by : arturdev
// Copyright (c) 2020 arturdev. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
import UIKit
internal extension UIWindow {
static var keyWindow: UIWindow? {
return UIApplication.shared.windows.filter({$0.isKeyWindow}).first
}
var width: CGFloat {
#if targetEnvironment(macCatalyst)
return frame.width
#else
let isPortrait = UIDevice.current.orientation.isPortrait || !UIDevice.current.orientation.isValidInterfaceOrientation
return isPortrait ? min(frame.width, frame.height) : max(frame.width, frame.height)
#endif
}
var height: CGFloat {
#if targetEnvironment(macCatalyst)
return frame.height
#else
let isPortrait = UIDevice.current.orientation.isPortrait || !UIDevice.current.orientation.isValidInterfaceOrientation
return isPortrait ? max(frame.width, frame.height) : min(frame.width, frame.height)
#endif
}
}
public protocol AMSlideMenuDelegate: class {
func leftMenuWillShow()
func leftMenuDidShow()
func leftMenuWillHide()
func leftMenuDidHide()
func rightMenuWillShow()
func rightMenuDidShow()
func rightMenuWillHide()
func rightMenuDidHide()
}
//Making the protocol methods optional
public extension AMSlideMenuDelegate {
func leftMenuWillShow() {}
func leftMenuDidShow() {}
func leftMenuWillHide() {}
func leftMenuDidHide() {}
func rightMenuWillShow() {}
func rightMenuDidShow() {}
func rightMenuWillHide() {}
func rightMenuDidHide() {}
}
@IBDesignable
open class AMSlideMenuMainViewController: UIViewController {
public private(set) var leftMenuVC: UIViewController?
public private(set) var rightMenuVC: UIViewController?
public weak private(set) var contentVC: UIViewController?
@IBInspectable public var leftMenuSegueName: String?
@IBInspectable public var rightMenuSegueName: String?
@IBInspectable public var contentSegueName: String?
private var menuWidthDefaultMultiplier: CGFloat {
#if targetEnvironment(macCatalyst)
return 0.201
#else
return 0.801
#endif
}
@IBInspectable open var leftMenuWidth: CGFloat = 0
@IBInspectable open var rightMenuWidth: CGFloat = 0
open var animationDuration = TimeInterval(0.25)
open var animationOptions: AMSlidingAnimationOptions = [.slidingMenu, .dimmedBackground, .menuShadow, .blurBackground]
open var leftMenuPanGestureRecognizer: UIPanGestureRecognizer!
open var rightMenuPanGestureRecognizer: UIPanGestureRecognizer!
open var contentPanGestureRecognizer: UIPanGestureRecognizer!
open var contentTapGestureRecognizer: UITapGestureRecognizer!
open var panGestureWorkingAreaPercent: Float = 50
open var menuState: MenuState = .closed {
didSet {
overlayView.isUserInteractionEnabled = menuState != .closed
if menuState == .closed {
contentView.addGestureRecognizer(contentPanGestureRecognizer)
} else {
overlayView.addGestureRecognizer(contentPanGestureRecognizer)
}
}
}
open weak var delegate: AMSlideMenuDelegate?
private var _animator:AMSlidingAnimatorProtocol?
var animator: AMSlidingAnimatorProtocol {
if _animator == nil {
_animator = AMSlidingAnimatorFactory.animator(options: animationOptions, duration: animationDuration)
}
return _animator!
}
var contentView: UIView = {
let view = UIView()
view.backgroundColor = .clear
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
return view
}()
var overlayView: UIView = {
let view = UIView()
view.backgroundColor = .clear
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.isUserInteractionEnabled = false
view.layer.zPosition = CGFloat(Float.greatestFiniteMagnitude)
view.tag = 1000
return view
}()
override open func viewDidLoad() {
super.viewDidLoad()
setupContent()
NotificationCenter.default.addObserver(self,
selector: #selector(AMSlideMenuMainViewController.handleShowLeftMenuNote),
name: .showLeftMenu,
object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(AMSlideMenuMainViewController.handleShowRightMenuNote),
name: .showRightMenu,
object: nil)
DispatchQueue.main.async {
self.leftMenuWidth == 0 ? (self.leftMenuWidth = (UIWindow.keyWindow?.width ?? 0) * self.menuWidthDefaultMultiplier) : nil
self.rightMenuWidth == 0 ? (self.rightMenuWidth = (UIWindow.keyWindow?.width ?? 0) * self.menuWidthDefaultMultiplier) : nil
}
}
deinit {
NotificationCenter.default.removeObserver(self,
name: .showLeftMenu,
object: nil)
NotificationCenter.default.removeObserver(self,
name: .showRightMenu,
object: nil)
}
open override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
view.backgroundColor = UIColor(red: 0.9, green: 0.9, blue: 0.9, alpha: 1)
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.backgroundColor = .clear
label.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
label.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20).isActive = true
label.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20).isActive = true
label.minimumScaleFactor = 0.5
label.textColor = .white
label.text = "AMSlideMenuContainer"
view.addSubview(label)
}
open override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
setupGestures()
}
open override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
setupLeftMenu()
setupRightMenu()
addGestures()
}
open override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
let shouldUpdateLeftMenuWidth = leftMenuWidth == view.bounds.width * menuWidthDefaultMultiplier
let shouldUpdateRightMenuWidth = rightMenuWidth == view.bounds.width * menuWidthDefaultMultiplier
super.viewWillTransition(to: size, with: coordinator)
let updateBlock = { (_: Any) in
shouldUpdateLeftMenuWidth ? (self.leftMenuWidth = (UIWindow.keyWindow?.width ?? 0) * self.menuWidthDefaultMultiplier) : nil
shouldUpdateRightMenuWidth ? (self.rightMenuWidth = (UIWindow.keyWindow?.width ?? 0) * self.menuWidthDefaultMultiplier) : nil
self.updateLeftMenuFrame()
self.updateRightMenuFrame()
}
coordinator.animate(alongsideTransition: updateBlock, completion: updateBlock)
}
@objc open override func showLeftMenu(animated: Bool, completion handler: (()->Void)? = nil) {
guard let leftMenuVC = leftMenuVC else { return }
if menuState == .leftOpened {
handler?()
return
}
hideRightMenu(animated: false, completion: nil)
delegate?.leftMenuWillShow()
animator.animate(leftMenuView: leftMenuVC.view, contentView: contentView, progress: 1, animated: animated, completion: {
self.menuState = .leftOpened
self.delegate?.leftMenuDidShow()
handler?()
})
}
@objc open override func showRightMenu(animated: Bool, completion handler: (()->Void)? = nil) {
guard let rightMenuVC = rightMenuVC else { return }
if menuState == .rightOpened {
handler?()
return
}
hideLeftMenu(animated: false, completion: nil)
delegate?.rightMenuWillShow()
animator.animate(rightMenuView: rightMenuVC.view, contentView: contentView, progress: 1, animated: animated, completion: {
self.menuState = .rightOpened
self.delegate?.rightMenuDidShow()
handler?()
})
}
@objc open override func hideLeftMenu(animated: Bool, completion handler: (()->Void)? = nil) {
guard let leftMenuVC = leftMenuVC else { return }
if menuState == .closed {
handler?()
return
}
self.delegate?.leftMenuWillHide()
animator.animate(leftMenuView: leftMenuVC.view, contentView: contentView, progress: 0, animated: animated, completion: {
self.menuState = .closed
self.delegate?.leftMenuDidHide()
handler?()
})
}
@objc open override func hideRightMenu(animated: Bool, completion handler: (()->Void)? = nil) {
guard let rightMenuVC = rightMenuVC else { return }
if menuState == .closed {
handler?()
return
}
self.delegate?.rightMenuWillHide()
animator.animate(rightMenuView: rightMenuVC.view, contentView: contentView, progress: 0, animated: animated, completion: {
self.menuState = .closed
self.delegate?.rightMenuDidHide()
handler?()
})
}
open func switchContentTo(vc: UIViewController, animated: Bool = true) {
UIView.setAnimationsEnabled(animated)
let segue = AMContentSegue(identifier: "content", source: self, destination: vc)
segue.perform()
UIView.setAnimationsEnabled(true)
}
//MARK: - Internal
func setLeftMenu(_ leftMenuVC: UIViewController) {
self.leftMenuVC = leftMenuVC
}
func setRightMenu(_ rightMenuVC: UIViewController) {
self.rightMenuVC = rightMenuVC
}
func setContentVC(_ contentVC: UIViewController) {
self.contentVC = contentVC
}
func setupContent() {
guard let segueName = contentSegueName else { return }
contentView.frame = view.bounds
view.addSubview(contentView)
performSegue(withIdentifier: segueName, sender: self)
}
func setupLeftMenu() {
guard let segueName = leftMenuSegueName else { return }
overlayView.frame = contentView.bounds
contentView.addSubview(overlayView)
performSegue(withIdentifier: segueName, sender: self)
}
func setupRightMenu() {
guard let segueName = rightMenuSegueName else { return }
overlayView.frame = contentView.bounds
contentView.addSubview(overlayView)
performSegue(withIdentifier: segueName, sender: self)
}
func updateLeftMenuFrame() {
guard let window = UIWindow.keyWindow else {return}
let x = menuState == .leftOpened ? 0 : -leftMenuWidth
leftMenuVC?.view.frame = CGRect(x: x, y: 0, width: leftMenuWidth, height: window.height)
}
func updateRightMenuFrame() {
guard let window = UIWindow.keyWindow else {return}
let x = menuState == .rightOpened ? (window.width - rightMenuWidth) : window.width
rightMenuVC?.view.frame = CGRect(x: x, y: 0, width: rightMenuWidth, height: window.height)
}
func setupGestures() {
contentPanGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
contentPanGestureRecognizer.delegate = self
contentTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
leftMenuPanGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
leftMenuPanGestureRecognizer.delegate = self
rightMenuPanGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
rightMenuPanGestureRecognizer.delegate = self
}
func addGestures() {
contentView.addGestureRecognizer(contentPanGestureRecognizer)
overlayView.addGestureRecognizer(contentTapGestureRecognizer)
leftMenuVC?.view.addGestureRecognizer(leftMenuPanGestureRecognizer)
rightMenuVC?.view.addGestureRecognizer(rightMenuPanGestureRecognizer)
}
@objc func handleShowLeftMenuNote(_ note: Notification) {
showLeftMenu(animated: true, completion: nil)
}
@objc func handleShowRightMenuNote(_ note: Notification) {
showRightMenu(animated: true, completion: nil)
}
@objc func handleTap(_ tap: UITapGestureRecognizer) {
if tap == self.contentTapGestureRecognizer {
if menuState == .leftOpened {
hideLeftMenu(animated: true)
} else if menuState == .rightOpened {
hideRightMenu(animated: true)
}
}
}
@objc func handlePan(_ pan: UIPanGestureRecognizer) {
guard pan == self.leftMenuPanGestureRecognizer || pan == self.rightMenuPanGestureRecognizer || pan == self.contentPanGestureRecognizer else {
return
}
enum PanIntentMenu {
case left
case right
}
struct Holder {
static var firstNonZeroHorizontalVelocity: CGFloat = 0
static var lastNonZeroHorizontalVelocity: CGFloat = 0
}
//guard let leftMenuVC = leftMenuVC, let leftMenuView = leftMenuVC.view else { return }
var panIntentMenu: PanIntentMenu = .left
let leftMenuView = leftMenuVC?.view
let rightMenuView = rightMenuVC?.view
let panningView = pan.view
let translation = pan.translation(in: panningView)
let velocity = pan.velocity(in: view)
if velocity.x != 0 {
Holder.lastNonZeroHorizontalVelocity = velocity.x
if Holder.firstNonZeroHorizontalVelocity == 0 {
Holder.firstNonZeroHorizontalVelocity = velocity.x
}
}
if menuState == .closed {
panIntentMenu = Holder.firstNonZeroHorizontalVelocity < 0 ? .right : .left
} else if menuState == .leftOpened {
panIntentMenu = .left
} else if menuState == .rightOpened {
panIntentMenu = .right
}
if pan.state == .began {
if self.menuState == .closed {
if velocity.x > 0 {
self.updateLeftMenuFrame()
self.delegate?.leftMenuWillShow()
} else {
self.updateRightMenuFrame()
self.delegate?.rightMenuWillShow()
}
} else {
if velocity.x < 0, menuState == .leftOpened {
self.delegate?.leftMenuWillHide()
} else if velocity.x > 0, menuState == .rightOpened {
self.delegate?.rightMenuWillHide()
}
}
pan.setTranslation(.zero, in: panningView)
} else if pan.state == .ended || pan.state == .cancelled || pan.state == .failed {
if panIntentMenu == .left, let leftMenuView = leftMenuView {
let progressToAnimate: CGFloat = Holder.lastNonZeroHorizontalVelocity > 0 ? 1 : 0
animator.animate(leftMenuView: leftMenuView, contentView: contentView, progress: progressToAnimate, animated: true) {
if progressToAnimate == 0 {
//Closed
self.menuState = .closed
self.delegate?.leftMenuDidHide()
} else {
//Opened
self.menuState = .leftOpened
self.delegate?.leftMenuDidShow()
}
}
} else if panIntentMenu == .right, let rightMenuView = rightMenuView {
let progressToAnimate: CGFloat = Holder.lastNonZeroHorizontalVelocity < 0 ? 1 : 0
animator.animate(rightMenuView: rightMenuView, contentView: contentView, progress: progressToAnimate, animated: true) {
if progressToAnimate == 0 {
//Closed
self.menuState = .closed
self.delegate?.rightMenuDidHide()
} else {
//Opened
self.menuState = .rightOpened
self.delegate?.rightMenuDidShow()
}
}
}
Holder.firstNonZeroHorizontalVelocity = 0
} else {
var progress: CGFloat = 0
if panIntentMenu == .left, let leftMenuView = leftMenuView {
if menuState == .closed {
progress = translation.x / leftMenuWidth
} else {
progress = 1 + translation.x / leftMenuWidth
}
animator.animate(leftMenuView: leftMenuView,
contentView: contentView,
progress: progress,
animated: false,
completion: nil)
} else if panIntentMenu == .right, let rightMenuView = rightMenuView {
if menuState == .closed {
progress = -translation.x / rightMenuWidth
} else {
progress = 1 - translation.x / rightMenuWidth
}
animator.animate(rightMenuView: rightMenuView,
contentView: contentView,
progress: progress,
animated: false,
completion: nil)
}
}
}
}
extension AMSlideMenuMainViewController: UIGestureRecognizerDelegate {
open func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer == leftMenuPanGestureRecognizer || gestureRecognizer == rightMenuPanGestureRecognizer || gestureRecognizer == contentPanGestureRecognizer {
if let pan = gestureRecognizer as? UIPanGestureRecognizer {
let velocity = pan.velocity(in: pan.view)
let isHorizontalGesture = abs(velocity.y) < abs(velocity.x)
if !isHorizontalGesture {
return false
}
if menuState != .closed {
return true
}
let point = pan.location(in: contentView)
if velocity.x > 0 {
if let rightMenuFrame = rightMenuVC?.view.layer.presentation()?.frame, rightMenuFrame.origin.x < contentView.bounds.width {
print(rightMenuFrame)
return false
}
return point.x <= contentView.bounds.width * CGFloat(panGestureWorkingAreaPercent) / 100
} else {
if let leftMenuFrame = leftMenuVC?.view.layer.presentation()?.frame, leftMenuFrame.maxX > 0 {
return false
}
return point.x > contentView.bounds.width * (1 - CGFloat(panGestureWorkingAreaPercent) / 100)
}
}
}
return true
}
open func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
guard gestureRecognizer == self.contentPanGestureRecognizer else {return true}
let velocity = self.contentPanGestureRecognizer.velocity(in: self.contentPanGestureRecognizer.view)
let isHorizontalGesture = abs(velocity.y) < abs(velocity.x)
if otherGestureRecognizer.view is UITableView {
if isHorizontalGesture {
let directionIsLeft = velocity.x < 0
if directionIsLeft {
if menuState == .closed {
contentPanGestureRecognizer.isEnabled = false
contentPanGestureRecognizer.isEnabled = true
return true
}
} else {
let tableView = otherGestureRecognizer.view as! UITableView
let point = otherGestureRecognizer.location(in: tableView)
if let indexPath = tableView.indexPathForRow(at: point), let cell = tableView.cellForRow(at: indexPath) {
if (cell.isEditing) {
contentPanGestureRecognizer.isEnabled = false
contentPanGestureRecognizer.isEnabled = true
return true
}
}
}
}
} else {
if let c = NSClassFromString("UITableViewCellScrollView"), otherGestureRecognizer.view?.isKind(of: c) ?? false {
return true
}
}
return false
}
}
extension AMSlideMenuMainViewController {
public enum MenuState {
case closed
case leftOpened
case rightOpened
}
}
extension NSNotification.Name {
public static let showLeftMenu = NSNotification.Name("showLeftMenu")
public static let showRightMenu = NSNotification.Name("showRightMenu")
}