forked from splyza/SideMenuController
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSideMenuController.swift
More file actions
776 lines (598 loc) · 27.6 KB
/
Copy pathSideMenuController.swift
File metadata and controls
776 lines (598 loc) · 27.6 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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
//
// SideMenuController.swift
//
// Copyright (c) 2015 Teodor Patraş
//
// 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
// MARK: - Public methods -
public extension SideMenuController {
/**
Toggles the side pannel visible or not.
*/
public func toggleSidePanel() {
if !transitionInProgress {
if !sidePanelVisible {
prepareSidePanel(forDisplay: true)
}
animate(toReveal: !sidePanelVisible)
}
}
/**
Embeds a new side controller
- parameter controller: controller to be embedded
*/
public func embedSideController(controller: UIViewController) {
if sideViewController == nil {
sideViewController = controller
sideViewController.view.frame = sidePanel.bounds
sidePanel.addSubview(sideViewController.view)
addChildViewController(sideViewController)
sideViewController.didMoveToParentViewController(self)
sidePanel.hidden = true
}
}
/**
Embeds a new center controller.
- parameter controller: controller to be embedded
*/
public func embedCenterController(controller: UINavigationController) {
prepareCenterControllerForContainment(controller)
centerPanel.addSubview(controller.view)
if centerViewController == nil {
centerViewController = controller
addChildViewController(centerViewController)
centerViewController.didMoveToParentViewController(self)
}else{
centerViewController.willMoveToParentViewController(nil)
addChildViewController(controller)
let block: () -> () = { _ in
self.centerViewController.view.removeFromSuperview()
self.centerViewController.removeFromParentViewController()
controller.didMoveToParentViewController(self)
self.centerViewController = controller
}
if let animation = self.dynamicType.preferences.animating.transitionAnimator?.transitionAnimation {
CATransaction.begin()
CATransaction.setCompletionBlock(block)
controller.view.layer.addAnimation(animation, forKey: nil)
CATransaction.commit()
} else {
block()
}
if sidePanelVisible {
animate(toReveal: false)
}
}
}
}
public class SideMenuController: UIViewController, UIGestureRecognizerDelegate {
// MARK:- Custom types -
public enum SidePanelPosition {
case UnderCenterPanelLeft
case UnderCenterPanelRight
case OverCenterPanelLeft
case OverCenterPanelRight
var isPositionedUnder: Bool {
return self == UnderCenterPanelLeft || self == UnderCenterPanelRight
}
var isPositionedLeft: Bool {
return self == UnderCenterPanelLeft || self == OverCenterPanelLeft
}
}
public enum StatusBarBehaviour {
case None
case Fade
case ShowUnderlay
case VerticalSlide
case HorizontalSlide
}
public struct Preferences {
public struct Drawing {
public var menuButtonImage: UIImage?
public var sidePanelWidth: CGFloat = 300
public var sidePanelPosition = SidePanelPosition.UnderCenterPanelLeft
public var centerPanelOverlayColor = UIColor(hue:0.15, saturation:0.21, brightness:0.17, alpha:0.6)
public var panningEnabled = true
public var swipingEnabled = true
public var drawSideShadow = true
}
public struct Animating {
public var statusBarBehaviour = StatusBarBehaviour.VerticalSlide
public var reavealDuration = 0.3
public var hideDuration = 0.2
public var transitionAnimator: TransitionAnimatable.Type? = FadeAnimator.self
}
public var drawing = Drawing()
public var animating = Animating()
public init() {}
}
// MARK: - Properties -
private let navigationObservedKey = "barTintColor"
// MARK: Public
public static var preferences: Preferences = Preferences()
private(set) public var sidePanelVisible = false
// MARK: Private
private var sttusBarHidden: Bool = false
private var snapshotView: UIView?
private var navigationBar: UINavigationBar!
private var centerViewController: UINavigationController!
private var sideViewController: UIViewController!
private var statusBarUnderlay: UIView!
private var centerPanel: UIView!
private var sidePanel: UIView!
private var centerPanelOverlay: UIView!
private var leftSwipeRecognizer: UISwipeGestureRecognizer!
private var rightSwipeGesture: UISwipeGestureRecognizer!
private var panRecognizer: UIPanGestureRecognizer!
private var tapRecognizer: UITapGestureRecognizer!
private var transitionInProgress = false
private var flickVelocity: CGFloat = 0
private lazy var screenSize: CGSize = {
return UIScreen.mainScreen().bounds.size
}()
private lazy var sidePanelPosition: SidePanelPosition = {
return self.dynamicType.preferences.drawing.sidePanelPosition
}()
// MARK: Internal
// MARK: Computed
private var statusBarHeight: CGFloat {
return UIApplication.sharedApplication().statusBarFrame.size.height > 0 ? UIApplication.sharedApplication().statusBarFrame.size.height : 20
}
private var drawShadow: Bool {
return self.dynamicType.preferences.drawing.drawSideShadow
}
private var hidesStatusBar: Bool {
return [.VerticalSlide, .Fade, .HorizontalSlide].contains(self.dynamicType.preferences.animating.statusBarBehaviour)
}
private var showsStatusUnderlay: Bool {
guard self.dynamicType.preferences.animating.statusBarBehaviour == .ShowUnderlay else {
return false
}
if UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad {
return true
}
return screenSize.width < screenSize.height
}
private var canDisplaySideController: Bool {
return sideViewController != nil
}
private var centerPanelFrame: CGRect {
if sidePanelPosition.isPositionedUnder && sidePanelVisible {
let sidePanelWidth = self.dynamicType.preferences.drawing.sidePanelWidth
return CGRectMake(sidePanelPosition.isPositionedLeft ? sidePanelWidth : -sidePanelWidth, 0, screenSize.width, screenSize.height)
} else {
return CGRectMake(0, 0, screenSize.width, screenSize.height)
}
}
private var sidePanelFrame: CGRect {
var sidePanelFrame: CGRect
let panelWidth = self.dynamicType.preferences.drawing.sidePanelWidth
if sidePanelPosition.isPositionedUnder {
sidePanelFrame = CGRectMake(sidePanelPosition.isPositionedLeft ? 0 :
screenSize.width - panelWidth, 0, panelWidth, screenSize.height)
} else {
if sidePanelVisible {
sidePanelFrame = CGRectMake(sidePanelPosition.isPositionedLeft ? 0 : screenSize.width - panelWidth, 0, panelWidth, screenSize.height)
} else {
sidePanelFrame = CGRectMake(sidePanelPosition.isPositionedLeft ? -panelWidth : screenSize.width, 0, panelWidth, screenSize.height)
}
}
return sidePanelFrame
}
// MARK:- View lifecycle -
override public func viewDidLoad() {
super.viewDidLoad()
configureViews()
}
override public func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
screenSize = size
coordinator.animateAlongsideTransition({ _ in
self.centerPanel.frame = self.centerPanelFrame
self.sidePanel.frame = self.sidePanelFrame
self.statusBarUnderlay.hidden = !self.showsStatusUnderlay
if let overlay = self.centerPanelOverlay {
overlay.frame = self.centerPanelFrame
}
self.view.layoutIfNeeded()
}, completion: nil)
}
override public func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if let navBar = object as? UINavigationBar where context == &navigationContext {
configureNavigation(withBar: navBar)
} else {
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}
}
deinit{
centerViewController?.navigationBar.removeObserver(self, forKeyPath: navigationObservedKey)
}
// MARK: - Configurations -
private func configureViews(){
centerPanel = UIView(frame: CGRectMake(0, 0, screenSize.width, screenSize.height))
view.addSubview(centerPanel)
navigationBar = UINavigationBar(frame: CGRectMake(0, 0, screenSize.width, statusBarHeight))
centerPanel.addSubview(navigationBar)
navigationBar.removeHairline()
statusBarUnderlay = UIView(frame: CGRectMake(0, 0, screenSize.width, statusBarHeight))
view.addSubview(statusBarUnderlay)
statusBarUnderlay.alpha = 0
sidePanel = UIView(frame: sidePanelFrame)
view.addSubview(sidePanel)
sidePanel.clipsToBounds = true
if sidePanelPosition.isPositionedUnder {
view.sendSubviewToBack(sidePanel)
} else {
centerPanelOverlay = UIView(frame: centerPanel.frame)
centerPanelOverlay.backgroundColor = self.dynamicType.preferences.drawing.centerPanelOverlayColor
view.bringSubviewToFront(sidePanel)
}
configureGestureRecognizers()
view.bringSubviewToFront(statusBarUnderlay)
}
private func configureGestureRecognizers() {
tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap))
tapRecognizer.delegate = self
if sidePanelPosition.isPositionedUnder {
panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handleCenterPanelPan))
panRecognizer.delegate = self
centerPanel.addGestureRecognizer(panRecognizer)
centerPanel.addGestureRecognizer(tapRecognizer)
} else {
panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handleSidePanelPan))
panRecognizer.delegate = self
sidePanel.addGestureRecognizer(panRecognizer)
leftSwipeRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(handleLeftSwipe))
leftSwipeRecognizer.delegate = self
leftSwipeRecognizer.direction = UISwipeGestureRecognizerDirection.Left
rightSwipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(handleRightSwipe))
rightSwipeGesture.delegate = self
rightSwipeGesture.direction = UISwipeGestureRecognizerDirection.Right
centerPanelOverlay.addGestureRecognizer(tapRecognizer)
if sidePanelPosition.isPositionedLeft {
centerPanel.addGestureRecognizer(rightSwipeGesture)
centerPanelOverlay.addGestureRecognizer(leftSwipeRecognizer)
}else{
centerPanel.addGestureRecognizer(leftSwipeRecognizer)
centerPanelOverlay.addGestureRecognizer(rightSwipeGesture)
}
}
}
private func configureNavigation(withBar bar: UINavigationBar) {
bar.translucent = false
let animation = self.dynamicType.preferences.animating.transitionAnimator?.transitionAnimation
navigationBar.removeFromSuperview()
let block : () -> () = { _ in
self.navigationBar.tintColor = bar.tintColor
self.navigationBar.backgroundColor = bar.backgroundColor
self.navigationBar.barTintColor = bar.barTintColor
self.navigationBar.translucent = bar.translucent
}
if let animation = animation where animation.duration > 0 {
let transition = CATransition()
transition.duration = animation.duration
transition.type = kCATransitionFade
transition.timingFunction = animation.timingFunction
block()
navigationBar.layer.addAnimation(transition, forKey: nil)
} else {
block()
}
centerPanel.addSubview(navigationBar)
}
private var statusBarWindow: UIWindow? {
return UIApplication.sharedApplication().valueForKey("statusBarWindow") as? UIWindow
}
private func setStatusBar(hidden hidden: Bool) {
guard hidesStatusBar else {
return
}
let setting = self.dynamicType.preferences.animating.statusBarBehaviour
if setting == .HorizontalSlide {
// if hidden == false {
// snapshotView?.removeFromSuperview()
// snapshotView = nil
// UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: .Fade)
// } else {
// UIApplication.sharedApplication().statusBarHidden = hidden
// if snapshotView == nil {
// snapshotView = UIScreen.mainScreen().snapshotViewAfterScreenUpdates(false)
// centerPanel.addSubview(snapshotView!)
// }
// }
//
} else {
let animation: UIStatusBarAnimation = setting == .Fade ? .Fade : .Slide
UIApplication.sharedApplication().setStatusBarHidden(hidden, withAnimation: animation)
}
}
private func setStatusUnderlay(alpha alpha: CGFloat) {
guard showsStatusUnderlay else {
return
}
if let color = centerViewController.navigationBar.barTintColor where statusBarUnderlay.backgroundColor != color {
statusBarUnderlay.backgroundColor = color
}
statusBarUnderlay.alpha = alpha
}
// MARK:- Containment =
private func prepareCenterControllerForContainment(controller: UINavigationController){
addMenuButtonToController(controller)
centerViewController?.navigationBar.removeObserver(self, forKeyPath: navigationObservedKey)
controller.navigationBar.addObserver(self, forKeyPath: navigationObservedKey, options: .New, context: &navigationContext)
var frame = centerPanel.bounds
frame.origin.y = statusBarHeight
frame.size.height -= statusBarHeight
controller.view.frame = frame
configureNavigation(withBar: controller.navigationBar)
}
private var navigationContext = 1
private func addMenuButtonToController(controller: UINavigationController) {
guard let image = self.dynamicType.preferences.drawing.menuButtonImage else {
return
}
let button = UIButton(frame: CGRectMake(0, 0, 40, 40))
button.setImage(image, forState: UIControlState.Normal)
button.addTarget(self, action: #selector(toggleSidePanel), forControlEvents: UIControlEvents.TouchUpInside)
let item:UIBarButtonItem = UIBarButtonItem()
item.customView = button
let spacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
spacer.width = -10
if sidePanelPosition.isPositionedLeft {
controller.childViewControllers[0].navigationItem.leftBarButtonItems = [spacer, item]
}else{
controller.childViewControllers[0].navigationItem.rightBarButtonItems = [spacer, item]
}
}
private func prepareSidePanel(forDisplay display: Bool){
sidePanel.hidden = !display
if !sidePanelPosition.isPositionedUnder {
if display && centerPanelOverlay.superview == nil {
centerPanelOverlay.alpha = 0
view.insertSubview(self.centerPanelOverlay, belowSubview: self.sidePanel)
}else if !display {
centerPanelOverlay.removeFromSuperview()
}
} else {
setSideShadow(hidden: display)
}
}
private func animate(toReveal reveal: Bool){
transitionInProgress = true
sidePanelVisible = reveal
if reveal {
setStatusBar(hidden: reveal)
}
let hide = sidePanelPosition.isPositionedUnder ? setUnderSidePanelHidden : setAboveSidePanelHidden
hide(!reveal) { _ in
if !reveal {
self.setStatusBar(hidden: reveal)
self.prepareSidePanel(forDisplay: false)
}
self.transitionInProgress = false
self.centerViewController.view.userInteractionEnabled = !reveal
}
}
func handleTap() {
animate(toReveal: false)
}
// MARK:- .UnderCenterPanelLeft & Right -
private func setSideShadow(hidden hidden: Bool) {
guard drawShadow else {
return
}
if hidden {
centerPanel.layer.shadowOpacity = 0.0
} else {
centerPanel.layer.shadowOpacity = 0.8
}
}
private func setUnderSidePanelHidden(hidden: Bool, completion: (() -> ())? = nil) {
var centerPanelFrame = centerPanel.frame
if !hidden {
if sidePanelPosition.isPositionedLeft {
centerPanelFrame.origin.x = CGRectGetMaxX(sidePanel.frame)
}else{
centerPanelFrame.origin.x = CGRectGetMinX(sidePanel.frame) - CGRectGetWidth(centerPanel.frame)
}
} else {
centerPanelFrame.origin = CGPointZero
}
var duration = hidden ? self.dynamicType.preferences.animating.hideDuration : self.dynamicType.preferences.animating.reavealDuration
if abs(flickVelocity) > 0 {
let newDuration = NSTimeInterval(sidePanel.frame.size.width / abs(flickVelocity))
flickVelocity = 0
duration = min(newDuration, duration)
}
UIView.panelAnimation( duration, animations: { _ in
self.centerPanel.frame = centerPanelFrame
self.statusBarWindow?.frame = centerPanelFrame
self.setStatusUnderlay(alpha: hidden ? 0 : 1)
}) { _ in
if hidden {
self.setSideShadow(hidden: false)
}
completion?()
}
}
func handleCenterPanelPan(recognizer: UIPanGestureRecognizer){
guard canDisplaySideController else {
return
}
self.flickVelocity = recognizer.velocityInView(recognizer.view).x
let leftToRight = flickVelocity > 0
switch(recognizer.state) {
case .Began:
if !sidePanelVisible {
sidePanelVisible = true
prepareSidePanel(forDisplay: true)
setSideShadow(hidden: true)
}
setStatusBar(hidden: true)
case .Changed:
let translation = recognizer.translationInView(view).x
let sidePanelFrame = sidePanel.frame
// origin.x or origin.x + width
let xPoint: CGFloat = centerPanel.center.x + translation +
(sidePanelPosition.isPositionedLeft ? -1 : 1 ) * CGRectGetWidth(centerPanel.frame) / 2
if xPoint < CGRectGetMinX(sidePanelFrame) || xPoint > CGRectGetMaxX(sidePanelFrame){
return
}
var alpha: CGFloat
if sidePanelPosition.isPositionedLeft {
alpha = xPoint / CGRectGetWidth(sidePanelFrame)
}else{
alpha = 1 - (xPoint - CGRectGetMinX(sidePanelFrame)) / CGRectGetWidth(sidePanelFrame)
}
setStatusUnderlay(alpha: alpha)
centerPanel.center.x = centerPanel.center.x + translation
recognizer.setTranslation(CGPointZero, inView: view)
default:
if sidePanelVisible {
var reveal = true
let centerFrame = centerPanel.frame
let sideFrame = sidePanel.frame
let shouldOpenPercentage = CGFloat(0.2)
let shouldHidePercentage = CGFloat(0.8)
if sidePanelPosition.isPositionedLeft {
if leftToRight {
// opening
reveal = CGRectGetMinX(centerFrame) > CGRectGetWidth(sideFrame) * shouldOpenPercentage
} else{
// closing
reveal = CGRectGetMinX(centerFrame) > CGRectGetWidth(sideFrame) * shouldHidePercentage
}
}else{
if leftToRight {
//closing
reveal = CGRectGetMaxX(centerFrame) < CGRectGetMinX(sideFrame) + shouldOpenPercentage * CGRectGetWidth(sideFrame)
}else{
// opening
reveal = CGRectGetMaxX(centerFrame) < CGRectGetMinX(sideFrame) + shouldHidePercentage * CGRectGetWidth(sideFrame)
}
}
animate(toReveal: reveal)
}
}
}
// MARK:- .OverCenterPanelLeft & Right -
func handleSidePanelPan(recognizer: UIPanGestureRecognizer){
guard canDisplaySideController else {
return
}
flickVelocity = recognizer.velocityInView(recognizer.view).x
let leftToRight = flickVelocity > 0
let sidePanelWidth = CGRectGetWidth(sidePanel.frame)
switch recognizer.state {
case .Began:
prepareSidePanel(forDisplay: true)
setStatusBar(hidden: true)
case .Changed:
let translation = recognizer.translationInView(view).x
let xPoint: CGFloat = sidePanel.center.x + translation + (sidePanelPosition.isPositionedLeft ? 1 : -1) * sidePanelWidth / 2
var alpha: CGFloat
if sidePanelPosition.isPositionedLeft {
if xPoint <= 0 || xPoint > CGRectGetWidth(sidePanel.frame) {
return
}
alpha = xPoint / CGRectGetWidth(sidePanel.frame)
}else{
if xPoint <= screenSize.width - sidePanelWidth || xPoint >= screenSize.width {
return
}
alpha = 1 - (xPoint - (screenSize.width - sidePanelWidth)) / sidePanelWidth
}
setStatusUnderlay(alpha: alpha)
centerPanelOverlay.alpha = alpha
sidePanel.center.x = sidePanel.center.x + translation
recognizer.setTranslation(CGPointZero, inView: view)
default:
let shouldClose: Bool
if sidePanelPosition.isPositionedLeft {
shouldClose = !leftToRight && CGRectGetMaxX(sidePanel.frame) < sidePanelWidth
} else {
shouldClose = leftToRight && CGRectGetMinX(sidePanel.frame) > (screenSize.width - sidePanelWidth)
}
animate(toReveal: !shouldClose)
}
}
private func setAboveSidePanelHidden(hidden: Bool, completion: ((Void) -> Void)? = nil){
var destinationFrame = sidePanel.frame
if sidePanelPosition.isPositionedLeft {
if hidden {
destinationFrame.origin.x = -CGRectGetWidth(destinationFrame)
} else {
destinationFrame.origin.x = CGRectGetMinX(view.frame)
}
} else {
if hidden {
destinationFrame.origin.x = CGRectGetMaxX(view.frame)
} else {
destinationFrame.origin.x = CGRectGetMaxX(view.frame) - CGRectGetWidth(destinationFrame)
}
}
var duration = hidden ? self.dynamicType.preferences.animating.hideDuration : self.dynamicType.preferences.animating.reavealDuration
if abs(flickVelocity) > 0 {
let newDuration = NSTimeInterval (destinationFrame.size.width / abs(flickVelocity))
flickVelocity = 0
if newDuration < duration {
duration = newDuration
}
}
UIView.panelAnimation(duration, animations: { () -> () in
self.centerPanelOverlay.alpha = hidden ? 0 : 1
self.setStatusUnderlay(alpha: hidden ? 0 : 1)
self.sidePanel.frame = destinationFrame
}, completion: completion)
}
func handleLeftSwipe(){
handleHorizontalSwipe(toLeft: true)
}
func handleRightSwipe(){
handleHorizontalSwipe(toLeft: false)
}
func handleHorizontalSwipe(toLeft left: Bool) {
if (left && sidePanelPosition.isPositionedLeft) ||
(!left && !sidePanelPosition.isPositionedLeft) {
if sidePanelVisible {
animate(toReveal: false)
}
} else {
if !sidePanelVisible {
prepareSidePanel(forDisplay: true)
animate(toReveal: true)
}
}
}
// MARK:- UIGestureRecognizerDelegate -
public func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
switch gestureRecognizer {
case panRecognizer:
return self.dynamicType.preferences.drawing.panningEnabled
case tapRecognizer:
return sidePanelVisible
default:
if gestureRecognizer is UISwipeGestureRecognizer {
return self.dynamicType.preferences.drawing.swipingEnabled
}
return true
}
}
}