Commit 18a4375b by Daniel Dahan

right panel for SideNav now supports pan gesture

parent 7a577efb
...@@ -108,10 +108,10 @@ public class SideNavigationViewController: UIViewController, UIGestureRecognizer ...@@ -108,10 +108,10 @@ public class SideNavigationViewController: UIViewController, UIGestureRecognizer
private var originalPosition: CGPoint = CGPointZero private var originalPosition: CGPoint = CGPointZero
/** /**
A UIView property that is used internally to track A MaterialView property that is used internally to track
the currentView, either leftView or rightView. either leftView or rightView.
*/ */
private var currentView: UIView? private var currentView: MaterialView?
/** /**
A UIPanGestureRecognizer property internally used for the A UIPanGestureRecognizer property internally used for the
...@@ -550,17 +550,18 @@ public class SideNavigationViewController: UIViewController, UIGestureRecognizer ...@@ -550,17 +550,18 @@ public class SideNavigationViewController: UIViewController, UIGestureRecognizer
*/ */
public func openLeftView(velocity: CGFloat = 0) { public func openLeftView(velocity: CGFloat = 0) {
if enabledLeftView { if enabledLeftView {
toggleStatusBar(true)
backdropLayer.hidden = false
if let v: MaterialView = leftView { if let v: MaterialView = leftView {
toggleStatusBar(true)
backdropLayer.hidden = false
currentView = v
delegate?.sideNavigationViewWillOpen?(self) delegate?.sideNavigationViewWillOpen?(self)
MaterialAnimation.animateWithDuration(Double(0 == velocity ? animationDuration : fmax(0.1, fmin(1, Double(v.x / velocity)))), MaterialAnimation.animateWithDuration(Double(0 == velocity ? animationDuration : fmax(0.1, fmin(1, Double(v.x / velocity)))),
animations: { animations: {
v.position = CGPointMake(v.width / 2, v.height / 2) v.position = CGPointMake(v.width / 2, v.height / 2)
}) { [unowned self] in }) { [unowned self] in
self.userInteractionEnabled = false self.userInteractionEnabled = false
self.showLeftViewDepth() self.showDepth()
self.delegate?.sideNavigationViewDidOpen?(self) self.delegate?.sideNavigationViewDidOpen?(self)
} }
} }
...@@ -575,17 +576,18 @@ public class SideNavigationViewController: UIViewController, UIGestureRecognizer ...@@ -575,17 +576,18 @@ public class SideNavigationViewController: UIViewController, UIGestureRecognizer
*/ */
public func openRightView(velocity: CGFloat = 0) { public func openRightView(velocity: CGFloat = 0) {
if enabledRightView { if enabledRightView {
toggleStatusBar(true)
backdropLayer.hidden = false
if let v: MaterialView = rightView { if let v: MaterialView = rightView {
toggleStatusBar(true)
backdropLayer.hidden = false
currentView = v
delegate?.sideNavigationViewWillOpen?(self) delegate?.sideNavigationViewWillOpen?(self)
MaterialAnimation.animateWithDuration(Double(0 == velocity ? animationDuration : fmax(0.1, fmin(1, Double(v.x / velocity)))), MaterialAnimation.animateWithDuration(Double(0 == velocity ? animationDuration : fmax(0.1, fmin(1, Double(v.x / velocity)))),
animations: { animations: {
v.position = CGPointMake(self.view.bounds.width - v.width / 2, v.height / 2) v.position = CGPointMake(self.view.bounds.width - v.width / 2, v.height / 2)
}) { [unowned self] in }) { [unowned self] in
self.userInteractionEnabled = false self.userInteractionEnabled = false
self.showRightViewDepth() self.showDepth()
self.delegate?.sideNavigationViewDidOpen?(self) self.delegate?.sideNavigationViewDidOpen?(self)
} }
} }
...@@ -610,8 +612,9 @@ public class SideNavigationViewController: UIViewController, UIGestureRecognizer ...@@ -610,8 +612,9 @@ public class SideNavigationViewController: UIViewController, UIGestureRecognizer
v.position = CGPointMake(-v.width / 2, v.height / 2) v.position = CGPointMake(-v.width / 2, v.height / 2)
}) { [unowned self] in }) { [unowned self] in
self.userInteractionEnabled = true self.userInteractionEnabled = true
self.hideLeftViewDepth() self.hideDepth()
self.delegate?.sideNavigationViewDidClose?(self) self.delegate?.sideNavigationViewDidClose?(self)
self.currentView = nil
} }
} }
} }
...@@ -635,8 +638,9 @@ public class SideNavigationViewController: UIViewController, UIGestureRecognizer ...@@ -635,8 +638,9 @@ public class SideNavigationViewController: UIViewController, UIGestureRecognizer
v.position = CGPointMake(self.view.bounds.width + v.width / 2, v.height / 2) v.position = CGPointMake(self.view.bounds.width + v.width / 2, v.height / 2)
}) { [unowned self] in }) { [unowned self] in
self.userInteractionEnabled = true self.userInteractionEnabled = true
self.hideRightViewDepth() self.hideDepth()
self.delegate?.sideNavigationViewDidClose?(self) self.delegate?.sideNavigationViewDidClose?(self)
self.currentView = nil
} }
} }
} }
...@@ -661,39 +665,73 @@ public class SideNavigationViewController: UIViewController, UIGestureRecognizer ...@@ -661,39 +665,73 @@ public class SideNavigationViewController: UIViewController, UIGestureRecognizer
passed to the handler when recognized. passed to the handler when recognized.
*/ */
internal func handlePanGesture(recognizer: UIPanGestureRecognizer) { internal func handlePanGesture(recognizer: UIPanGestureRecognizer) {
if enabled { // Deterine which view to pan.
switch recognizer.state { if enabledRightView && (openedRightView || !openedLeftView && isPointContainedWithinRightViewThreshold(recognizer.locationInView(view))) {
case .Began: currentView = rightView
if !opened { if let v: MaterialView = currentView {
if isPointContainedWithinLeftViewThreshold(recognizer.locationInView(view)) { // Animate the panel.
switch recognizer.state {
case .Began:
backdropLayer.hidden = false
originalPosition = v.position
toggleStatusBar(true)
showDepth()
delegate?.sideNavigationViewPanDidBegin?(self, point: recognizer.locationInView(view))
case .Changed:
let translation: CGPoint = recognizer.translationInView(v)
let w: CGFloat = v.width
MaterialAnimation.animationDisabled { [unowned self] in
print("OX \(self.originalPosition.x) TX \(translation.x)")
v.position.x = self.originalPosition.x + translation.x < self.view.bounds.width - (w / 2) ? self.view.bounds.width - (w / 2) : self.originalPosition.x + translation.x
self.delegate?.sideNavigationViewPanDidChange?(self, point: recognizer.locationInView(self.view))
} }
case .Ended, .Cancelled, .Failed:
let point: CGPoint = recognizer.velocityInView(recognizer.view)
let x: CGFloat = point.x >= 1000 || point.x <= -1000 ? point.x : 0
delegate?.sideNavigationViewPanDidEnd?(self, point: recognizer.locationInView(view))
if v.x <= CGFloat(floor(-rightViewWidth)) + rightViewThreshold || point.x <= -1000 {
closeRightView(x)
} else {
openRightView(x)
}
case .Possible:break
} }
backdropLayer.hidden = false }
originalPosition = leftView!.position } else if enabledLeftView {
toggleStatusBar(true) currentView = leftView
showLeftViewDepth() if let v: MaterialView = currentView {
delegate?.sideNavigationViewPanDidBegin?(self, point: recognizer.locationInView(view)) // Animate the panel.
case .Changed: switch recognizer.state {
let translation: CGPoint = recognizer.translationInView(leftView) case .Began:
let w: CGFloat = leftView!.width backdropLayer.hidden = false
originalPosition = v.position
MaterialAnimation.animationDisabled { [unowned self] in toggleStatusBar(true)
self.leftView!.position.x = self.originalPosition.x + translation.x > (w / 2) ? (w / 2) : self.originalPosition.x + translation.x showDepth()
self.delegate?.sideNavigationViewPanDidChange?(self, point: recognizer.locationInView(self.view)) delegate?.sideNavigationViewPanDidBegin?(self, point: recognizer.locationInView(view))
} case .Changed:
case .Ended, .Cancelled, .Failed: let translation: CGPoint = recognizer.translationInView(v)
let point: CGPoint = recognizer.velocityInView(recognizer.view) let w: CGFloat = v.width
let x: CGFloat = point.x >= 1000 || point.x <= -1000 ? point.x : 0
MaterialAnimation.animationDisabled { [unowned self] in
delegate?.sideNavigationViewPanDidEnd?(self, point: recognizer.locationInView(view)) v.position.x = self.originalPosition.x + translation.x > (w / 2) ? (w / 2) : self.originalPosition.x + translation.x
self.delegate?.sideNavigationViewPanDidChange?(self, point: recognizer.locationInView(self.view))
if leftView!.x <= CGFloat(floor(-leftViewWidth)) + leftViewThreshold || point.x <= -1000 { }
closeLeftView(x) case .Ended, .Cancelled, .Failed:
} else { let point: CGPoint = recognizer.velocityInView(recognizer.view)
openLeftView(x) let x: CGFloat = point.x >= 1000 || point.x <= -1000 ? point.x : 0
delegate?.sideNavigationViewPanDidEnd?(self, point: recognizer.locationInView(view))
if v.x <= CGFloat(floor(-leftViewWidth)) + leftViewThreshold || point.x <= -1000 {
closeLeftView(x)
} else {
openLeftView(x)
}
case .Possible:break
} }
case .Possible:break
} }
} }
} }
...@@ -898,46 +936,18 @@ public class SideNavigationViewController: UIViewController, UIGestureRecognizer ...@@ -898,46 +936,18 @@ public class SideNavigationViewController: UIViewController, UIGestureRecognizer
return CGRectContainsPoint(container.frame, point) return CGRectContainsPoint(container.frame, point)
} }
/** /// A method that adds the depth to the currentView.
A method that adds the depth to the leftView depth property. private func showDepth() {
*/ if let v: MaterialView = currentView {
private func showLeftViewDepth() {
if let v: MaterialView = leftView {
MaterialAnimation.animationDisabled { [unowned self] in MaterialAnimation.animationDisabled { [unowned self] in
v.depth = self.depth v.depth = self.depth
} }
} }
} }
/** /// A method that removes the depth from the currentView.
A method that adds the depth to the rightView depth property. private func hideDepth() {
*/ if let v: MaterialView = currentView {
private func showRightViewDepth() {
if let v: MaterialView = rightView {
MaterialAnimation.animationDisabled {
v.depth = self.depth
}
}
}
/**
A method that removes the depth from the leftView depth
property.
*/
private func hideLeftViewDepth() {
if let v: MaterialView = leftView {
MaterialAnimation.animationDisabled {
v.depth = .None
}
}
}
/**
A method that removes the depth from the rightView depth
property.
*/
private func hideRightViewDepth() {
if let v: MaterialView = rightView {
MaterialAnimation.animationDisabled { MaterialAnimation.animationDisabled {
v.depth = .None v.depth = .None
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment