Skip to content

Commit 9d03ba4

Browse files
author
Daniel Dahan
committed
issue-997: Fixed NavigationDrawerController where swiping off d
evice caused a partial correct state.
1 parent 5456cd7 commit 9d03ba4

File tree

3 files changed

+66
-11
lines changed

3 files changed

+66
-11
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.12.19
2+
3+
* [issue-997](https://github.com/CosmicMind/Material/issues/977): Fixed NavigationDrawerController where swiping off device caused a partial correct state.
4+
15
## 2.12.18
26

37
* Fixed layout issues in CollectionView, where the sizing was not correctlly being initialized.

Material.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'Material'
3-
s.version = '2.12.18'
3+
s.version = '2.12.19'
44
s.license = 'BSD-3-Clause'
55
s.summary = 'A UI/UX framework for creating beautiful applications.'
66
s.homepage = 'http://materialswift.com'

Sources/iOS/NavigationDrawerController.swift

+61-10
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ public protocol NavigationDrawerControllerDelegate {
138138

139139
@objc(NavigationDrawerController)
140140
open class NavigationDrawerController: TransitionController {
141+
/// A boolean indicating if the panel is animating.
142+
fileprivate var isAnimating = false
143+
141144
/**
142145
A CGFloat property that is used internally to track
143146
the original (x) position of the container view when panning.
@@ -216,6 +219,7 @@ open class NavigationDrawerController: TransitionController {
216219
if nil != leftView {
217220
isLeftViewEnabled = value
218221
}
222+
219223
if nil != rightView {
220224
isRightViewEnabled = value
221225
}
@@ -429,21 +433,21 @@ open class NavigationDrawerController: TransitionController {
429433
v.frame.size.width = leftViewWidth
430434
v.frame.size.height = view.bounds.height
431435
leftViewThreshold = leftViewWidth / 2
432-
if let vc = leftViewController {
436+
437+
if let vc = leftViewController {
438+
vc.view.frame = v.bounds
433439
vc.view.frame.size.width = leftViewWidth
434-
vc.view.frame.size.height = v.bounds.height
435-
vc.view.center = CGPoint(x: leftViewWidth / 2, y: v.bounds.height / 2)
436440
}
437441
}
438442

439443
if let v = rightView {
440444
v.frame.size.width = rightViewWidth
441445
v.frame.size.height = view.bounds.height
442446
rightViewThreshold = view.bounds.width - rightViewWidth / 2
447+
443448
if let vc = rightViewController {
444-
vc.view.frame.size.width = rightViewWidth
445-
vc.view.frame.size.height = v.bounds.height
446-
vc.view.center = CGPoint(x: rightViewWidth / 2, y: v.bounds.height / 2)
449+
vc.view.frame = v.bounds
450+
vc.view.frame.size.width = rightViewWidth
447451
}
448452
}
449453

@@ -527,6 +531,7 @@ open class NavigationDrawerController: TransitionController {
527531
v.bounds.size.width = width
528532
v.layer.position.x = -width / 2
529533
s.rootViewController.view.alpha = 1
534+
530535
}) { [weak self, v = v] _ in
531536
guard let s = self else {
532537
return
@@ -536,6 +541,7 @@ open class NavigationDrawerController: TransitionController {
536541
s.layoutSubviews()
537542
s.hideView(container: v)
538543
}
544+
539545
} else {
540546
UIView.animate(withDuration: duration,
541547
animations: { [weak self, v = v] in
@@ -546,6 +552,7 @@ open class NavigationDrawerController: TransitionController {
546552
v.bounds.size.width = width
547553
v.layer.position.x = width / 2
548554
s.rootViewController.view.alpha = 0.5
555+
549556
}) { [weak self, v = v] _ in
550557
guard let s = self else {
551558
return
@@ -563,6 +570,7 @@ open class NavigationDrawerController: TransitionController {
563570
hideView(container: v)
564571
v.layer.position.x = -v.bounds.width / 2
565572
rootViewController.view.alpha = 1
573+
566574
} else {
567575
showView(container: v)
568576
v.layer.position.x = width / 2
@@ -609,6 +617,7 @@ open class NavigationDrawerController: TransitionController {
609617
v.bounds.size.width = width
610618
v.layer.position.x = s.view.bounds.width + width / 2
611619
s.rootViewController.view.alpha = 1
620+
612621
}) { [weak self, v = v] _ in
613622
guard let s = self else {
614623
return
@@ -618,6 +627,7 @@ open class NavigationDrawerController: TransitionController {
618627
s.layoutSubviews()
619628
s.hideView(container: v)
620629
}
630+
621631
} else {
622632
UIView.animate(withDuration: duration,
623633
animations: { [weak self, v = v] in
@@ -628,6 +638,7 @@ open class NavigationDrawerController: TransitionController {
628638
v.bounds.size.width = width
629639
v.layer.position.x = s.view.bounds.width - width / 2
630640
s.rootViewController.view.alpha = 0.5
641+
631642
}) { [weak self, v = v] _ in
632643
guard let s = self else {
633644
return
@@ -645,6 +656,7 @@ open class NavigationDrawerController: TransitionController {
645656
hideView(container: v)
646657
v.layer.position.x = view.bounds.width + v.bounds.width / 2
647658
rootViewController.view.alpha = 1
659+
648660
} else {
649661
showView(container: v)
650662
v.layer.position.x = view.bounds.width - width / 2
@@ -684,6 +696,10 @@ open class NavigationDrawerController: TransitionController {
684696
leftView. Defaults to 0.
685697
*/
686698
open func openLeftView(velocity: CGFloat = 0) {
699+
guard !isAnimating else {
700+
return
701+
}
702+
687703
guard isLeftViewEnabled else {
688704
return
689705
}
@@ -692,6 +708,8 @@ open class NavigationDrawerController: TransitionController {
692708
return
693709
}
694710

711+
isAnimating = true
712+
695713
hideStatusBar()
696714
showView(container: v)
697715

@@ -707,11 +725,13 @@ open class NavigationDrawerController: TransitionController {
707725

708726
v.layer.position.x = v.bounds.width / 2
709727
s.rootViewController.view.alpha = 0.5
728+
710729
}) { [weak self] _ in
711730
guard let s = self else {
712731
return
713732
}
714733

734+
s.isAnimating = false
715735
s.delegate?.navigationDrawerController?(navigationDrawerController: s, didOpen: .left)
716736
}
717737
}
@@ -723,6 +743,10 @@ open class NavigationDrawerController: TransitionController {
723743
leftView. Defaults to 0.
724744
*/
725745
open func openRightView(velocity: CGFloat = 0) {
746+
guard !isAnimating else {
747+
return
748+
}
749+
726750
guard isRightViewEnabled else {
727751
return
728752
}
@@ -731,6 +755,8 @@ open class NavigationDrawerController: TransitionController {
731755
return
732756
}
733757

758+
isAnimating = true
759+
734760
hideStatusBar()
735761
showView(container: v)
736762

@@ -746,11 +772,14 @@ open class NavigationDrawerController: TransitionController {
746772

747773
v.layer.position.x = s.view.bounds.width - v.bounds.width / 2
748774
s.rootViewController.view.alpha = 0.5
775+
749776
}) { [weak self] _ in
750777
guard let s = self else {
751778
return
752779
}
753780

781+
s.isAnimating = false
782+
754783
s.delegate?.navigationDrawerController?(navigationDrawerController: s, didOpen: .right)
755784
}
756785
}
@@ -762,6 +791,10 @@ open class NavigationDrawerController: TransitionController {
762791
leftView. Defaults to 0.
763792
*/
764793
open func closeLeftView(velocity: CGFloat = 0) {
794+
guard !isAnimating else {
795+
return
796+
}
797+
765798
guard isLeftViewEnabled else {
766799
return
767800
}
@@ -770,7 +803,7 @@ open class NavigationDrawerController: TransitionController {
770803
return
771804
}
772805

773-
isUserInteractionEnabled = true
806+
isAnimating = true
774807

775808
delegate?.navigationDrawerController?(navigationDrawerController: self, willClose: .left)
776809

@@ -782,6 +815,7 @@ open class NavigationDrawerController: TransitionController {
782815

783816
v.layer.position.x = -v.bounds.width / 2
784817
s.rootViewController.view.alpha = 1
818+
785819
}) { [weak self, v = v] _ in
786820
guard let s = self else {
787821
return
@@ -790,6 +824,9 @@ open class NavigationDrawerController: TransitionController {
790824
s.hideView(container: v)
791825
s.toggleStatusBar()
792826

827+
s.isAnimating = false
828+
s.isUserInteractionEnabled = true
829+
793830
s.delegate?.navigationDrawerController?(navigationDrawerController: s, didClose: .left)
794831
}
795832
}
@@ -801,6 +838,10 @@ open class NavigationDrawerController: TransitionController {
801838
leftView. Defaults to 0.
802839
*/
803840
open func closeRightView(velocity: CGFloat = 0) {
841+
guard !isAnimating else {
842+
return
843+
}
844+
804845
guard isRightViewEnabled else {
805846
return
806847
}
@@ -809,7 +850,7 @@ open class NavigationDrawerController: TransitionController {
809850
return
810851
}
811852

812-
isUserInteractionEnabled = true
853+
isAnimating = true
813854

814855
delegate?.navigationDrawerController?(navigationDrawerController: self, willClose: .right)
815856

@@ -821,6 +862,7 @@ open class NavigationDrawerController: TransitionController {
821862

822863
v.layer.position.x = s.view.bounds.width + v.bounds.width / 2
823864
s.rootViewController.view.alpha = 1
865+
824866
}) { [weak self, v = v] _ in
825867
guard let s = self else {
826868
return
@@ -829,6 +871,9 @@ open class NavigationDrawerController: TransitionController {
829871
s.hideView(container: v)
830872
s.toggleStatusBar()
831873

874+
s.isAnimating = false
875+
s.isUserInteractionEnabled = true
876+
832877
s.delegate?.navigationDrawerController?(navigationDrawerController: s, didClose: .right)
833878
}
834879
}
@@ -1144,6 +1189,7 @@ extension NavigationDrawerController: UIGestureRecognizerDelegate {
11441189
showView(container: v)
11451190

11461191
delegate?.navigationDrawerController?(navigationDrawerController: self, didBeginPanAt: point, position: .left)
1192+
11471193
case .changed:
11481194
let w = v.bounds.width
11491195
let translationX = recognizer.translation(in: v).x
@@ -1158,17 +1204,19 @@ extension NavigationDrawerController: UIGestureRecognizerDelegate {
11581204
}
11591205

11601206
delegate?.navigationDrawerController?(navigationDrawerController: self, didChangePanAt: point, position: .left)
1207+
11611208
case .ended, .cancelled, .failed:
11621209
let p = recognizer.velocity(in: recognizer.view)
1163-
let x = p.x >= 500 || p.x <= -500 ? p.x : 0
1210+
let x = p.x >= 1000 || p.x <= -1000 ? p.x : 0
11641211

11651212
delegate?.navigationDrawerController?(navigationDrawerController: self, didEndPanAt: point, position: .left)
11661213

1167-
if v.frame.origin.x <= -leftViewWidth + leftViewThreshold || x < -500 {
1214+
if v.frame.origin.x <= -leftViewWidth + leftViewThreshold || x < -1000 {
11681215
closeLeftView(velocity: x)
11691216
} else {
11701217
openLeftView(velocity: x)
11711218
}
1219+
11721220
case .possible:break
11731221
}
11741222
}
@@ -1198,6 +1246,7 @@ extension NavigationDrawerController: UIGestureRecognizerDelegate {
11981246
showView(container: v)
11991247

12001248
delegate?.navigationDrawerController?(navigationDrawerController: self, didBeginPanAt: point, position: .right)
1249+
12011250
case .changed:
12021251
let w = v.bounds.width
12031252
let translationX = recognizer.translation(in: v).x
@@ -1212,6 +1261,7 @@ extension NavigationDrawerController: UIGestureRecognizerDelegate {
12121261
}
12131262

12141263
delegate?.navigationDrawerController?(navigationDrawerController: self, didChangePanAt: point, position: .right)
1264+
12151265
case .ended, .cancelled, .failed:
12161266
let p = recognizer.velocity(in: recognizer.view)
12171267
let x = p.x >= 1000 || p.x <= -1000 ? p.x : 0
@@ -1223,6 +1273,7 @@ extension NavigationDrawerController: UIGestureRecognizerDelegate {
12231273
} else {
12241274
openRightView(velocity: x)
12251275
}
1276+
12261277
case .possible:break
12271278
}
12281279
}

0 commit comments

Comments
 (0)