Commit bfe9ed9f by danieldahan

updated SideNav animation

parent 66bba074
......@@ -257,7 +257,6 @@
9693BF841BCB0DEE0087054A /* Extensions */,
96B57D4C1B90AF6A00DE7BBB /* Theme */,
9AAC38521B89553800FE6B2D /* Font */,
963C7A681BD4726900D175C5 /* Text */,
65AD15E81BC05B0E0068AF9C /* Layer */,
65B965851B8BEEB00055B139 /* View */,
965C17C21BC7258100B1059A /* Navigation */,
......@@ -297,13 +296,6 @@
name = "Supporting Files";
sourceTree = "<group>";
};
963C7A681BD4726900D175C5 /* Text */ = {
isa = PBXGroup;
children = (
);
name = Text;
sourceTree = "<group>";
};
964B17B21BBA445D002A9CA0 /* Layout */ = {
isa = PBXGroup;
children = (
......
......@@ -42,6 +42,11 @@ public class SideNavigationViewController: UIViewController, UIGestureRecognizer
private lazy var originalPosition: CGPoint = CGPointZero
//
// :name: sideView
//
private lazy var sideView: MaterialView = MaterialView()
//
// :name: sidePanGesture
//
internal var sidePanGesture: UIPanGestureRecognizer?
......@@ -117,15 +122,10 @@ public class SideNavigationViewController: UIViewController, UIGestureRecognizer
:name: opened
*/
public var opened: Bool {
return sideView.position.x == sideViewControllerWidth / 2
return sideView.x != -sideViewControllerWidth
}
/**
:name: sideView
*/
public private(set) var sideView: MaterialView = MaterialView()
/**
:name: maintViewController
*/
public var mainViewController: UIViewController!
......@@ -138,26 +138,7 @@ public class SideNavigationViewController: UIViewController, UIGestureRecognizer
/**
:name: sideViewControllerWidth
*/
public var sideViewControllerWidth: CGFloat = 240 {
didSet {
horizontalThreshold = sideViewControllerWidth / 2
layoutSideView()
}
}
/**
:name: init
*/
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
/**
:name: init
*/
public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
public private(set) var sideViewControllerWidth: CGFloat = 240
/**
:name: init
......@@ -167,26 +148,57 @@ public class SideNavigationViewController: UIViewController, UIGestureRecognizer
self.mainViewController = mainViewController
self.sideViewController = sideViewController
prepareView()
prepareMainView()
prepareSideView()
}
//
// :name: viewDidLoad
//
/**
:name: viewDidLoad
*/
public override func viewDidLoad() {
super.viewDidLoad()
edgesForExtendedLayout = .None
}
/**
:name: viewWillLayoutSubviews
*/
public override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
layoutBackdropLayer()
layoutSideView()
if let v: MaterialView = sideView {
sideViewController?.view.frame = v.bounds
sideViewController?.view.center = CGPointMake(v.width / 2, v.height / 2)
}
}
/**
:name: toggleSideViewContainer
:name: setSideViewControllerWidth
*/
public func toggleSideViewContainer(velocity: CGFloat = 0) {
public func setSideViewControllerWidth(width: CGFloat, hidden: Bool, animated: Bool) {
sideViewControllerWidth = width
let w: CGFloat = (hidden ? -width : width) / 2
if animated {
MaterialAnimation.animationWithDuration(0.25, animations: {
self.sideView.width = width
self.sideView.position.x = w
}) {
self.userInteractionEnabled = false
}
} else {
MaterialAnimation.animationDisabled({
self.sideView.width = width
self.sideView.position.x = w
})
}
}
/**
:name: toggle
*/
public func toggle(velocity: CGFloat = 0) {
opened ? close(velocity) : open(velocity)
}
......@@ -194,10 +206,13 @@ public class SideNavigationViewController: UIViewController, UIGestureRecognizer
:name: open
*/
public func open(velocity: CGFloat = 0) {
let w: CGFloat = sideView.width
let h: CGFloat = sideView.height
let d: Double = Double(0 == velocity ? animationDuration : fmax(0.1, fmin(1, Double(sideView.x / velocity))))
toggleStatusBar(true)
MaterialAnimation.animationWithDuration(Double(0 == velocity ? animationDuration : fmax(0.1, fmin(1, Double(sideView.x / velocity)))),
animations: {
self.sideView.position.x = self.sideViewControllerWidth / 2
MaterialAnimation.animationWithDuration(d, animations: {
self.sideView.position = CGPointMake(w / 2, h / 2)
self.backdropLayer.hidden = false
}) {
self.userInteractionEnabled = false
......@@ -208,10 +223,13 @@ public class SideNavigationViewController: UIViewController, UIGestureRecognizer
:name: close
*/
public func close(velocity: CGFloat = 0) {
let w: CGFloat = sideView.width
let h: CGFloat = sideView.height
let d: Double = Double(0 == velocity ? animationDuration : fmax(0.1, fmin(1, Double(sideView.x / velocity))))
toggleStatusBar(false)
MaterialAnimation.animationWithDuration(Double(0 == velocity ? animationDuration : fmax(0.1, fmin(1, Double(sideView.x / velocity)))),
animations: {
self.sideView.position.x = -self.sideViewControllerWidth / 2
MaterialAnimation.animationWithDuration(d, animations: {
self.sideView.position = CGPointMake(-w / 2, h / 2)
self.backdropLayer.hidden = true
}) {
self.userInteractionEnabled = true
......@@ -234,14 +252,28 @@ public class SideNavigationViewController: UIViewController, UIGestureRecognizer
return false
}
/**
:name: prepareGestures
*/
private func prepareGestures(inout pan: UIPanGestureRecognizer?, panSelector: Selector, inout tap: UITapGestureRecognizer?, tapSelector: Selector) {
if nil == pan {
pan = UIPanGestureRecognizer(target: self, action: panSelector)
pan!.delegate = self
view.addGestureRecognizer(pan!)
}
if nil == tap {
tap = UITapGestureRecognizer(target: self, action: tapSelector)
tap!.delegate = self
view.addGestureRecognizer(tap!)
}
}
//
// :name: prepareView
//
internal func prepareView() {
backdropColor = MaterialColor.black
prepareBackdropLayer()
prepareMainView()
prepareSideView()
}
//
......@@ -249,53 +281,43 @@ public class SideNavigationViewController: UIViewController, UIGestureRecognizer
//
internal func prepareMainView() {
prepareViewControllerWithinContainer(mainViewController, container: view)
mainViewController.view.translatesAutoresizingMaskIntoConstraints = false
MaterialLayout.alignToParent(view, child: mainViewController.view)
}
//
// :name: prepareSideView
//
internal func prepareSideView() {
MaterialAnimation.animationDisabled {
self.sideView.frame = CGRectMake(0, 0, self.sideViewControllerWidth, self.view.bounds.height)
self.sideView.position = CGPointMake(-self.sideViewControllerWidth / 2, self.view.bounds.height / 2)
}
// container
sideView.frame = CGRectMake(0, 0, sideViewControllerWidth, view.frame.height)
sideView.backgroundColor = MaterialColor.blue.accent3
sideView.zPosition = 1000
view.addSubview(sideView)
prepareLeftGestures()
prepareViewControllerWithinContainer(sideViewController, container: sideView)
}
//
// :name: layoutSideView
//
internal func layoutSideView() {
MaterialAnimation.animationDisabled {
self.sideView.width = self.sideViewControllerWidth
self.sideView.height = self.view.bounds.height
self.sideView.position = CGPointMake((self.opened ? 1 : -1) * self.sideViewControllerWidth / 2, self.view.bounds.height / 2)
self.sideViewController.view.frame = self.sideView.bounds
self.sideViewController.view.center = CGPointMake(self.sideView.width / 2, self.sideView.height / 2)
}
MaterialAnimation.animationDisabled({
self.sideView.position.x = -self.sideViewControllerWidth / 2
self.sideView.zPosition = 1000
})
prepareViewControllerWithinContainer(sideViewController!, container: sideView)
// gestures
prepareGestures()
}
//
// :name: handlePanGesture
// :name: handleLeftPanGesture
//
internal func handlePanGesture(recognizer: UIPanGestureRecognizer) {
internal func handleLeftPanGesture(recognizer: UIPanGestureRecognizer) {
switch recognizer.state {
case .Began:
toggleStatusBar(true)
originalPosition = sideView.position
toggleStatusBar(true)
backdropLayer.hidden = false
case .Changed:
let translation: CGPoint = recognizer.translationInView(view)
let x: CGFloat = self.sideViewControllerWidth / 2
MaterialAnimation.animationDisabled {
self.sideView.position.x = self.originalPosition.x + translation.x > x ? x : self.originalPosition.x + translation.x
}
let translation: CGPoint = recognizer.translationInView(sideView)
let w: CGFloat = sideView.width
MaterialAnimation.animationDisabled({
self.sideView.position.x = self.originalPosition.x + translation.x > (w / 2) ? (w / 2) : self.originalPosition.x + translation.x
})
case .Ended:
let point: CGPoint = recognizer.velocityInView(recognizer.view)
let x: CGFloat = point.x >= 1000 || point.x <= -1000 ? point.x : 0
......@@ -309,42 +331,24 @@ public class SideNavigationViewController: UIViewController, UIGestureRecognizer
}
//
// :name: handleTapGesture
// :name: handleLeftTapGesture
//
internal func handleTapGesture(recognizer: UIPanGestureRecognizer) {
internal func handleLeftTapGesture(recognizer: UIPanGestureRecognizer) {
if opened {
close()
}
}
//
// :name: prepareGestures
//
private func prepareGestures(inout pan: UIPanGestureRecognizer?, panSelector: Selector, inout tap: UITapGestureRecognizer?, tapSelector: Selector) {
if nil == pan {
pan = UIPanGestureRecognizer(target: self, action: panSelector)
pan!.delegate = self
view.addGestureRecognizer(pan!)
}
if nil == tap {
tap = UITapGestureRecognizer(target: self, action: tapSelector)
tap!.delegate = self
view.addGestureRecognizer(tap!)
}
}
//
// :name: removeGestures
//
private func removeGestures(inout pan: UIPanGestureRecognizer?, inout tap: UITapGestureRecognizer?) {
if let v = pan {
view.removeGestureRecognizer(v)
v.delegate = nil
if let g = pan {
view.removeGestureRecognizer(g)
pan = nil
}
if let v = tap {
view.removeGestureRecognizer(v)
v.delegate = nil
if let g = tap {
view.removeGestureRecognizer(g)
tap = nil
}
}
......@@ -379,14 +383,14 @@ public class SideNavigationViewController: UIViewController, UIGestureRecognizer
// :name: gesturePanSideViewController
//
private func gesturePanSideViewController(gesture: UIGestureRecognizer, withTouchPoint point: CGPoint) -> Bool {
return opened || enabled && isPointContainedWithinHorizontalThreshold(point)
return opened || enabled && isLeftPointContainedWithinRect(point)
}
//
// :name: isPointContainedWithinHorizontalThreshold
// :name: isLeftPointContainedWithinRect
//
private func isPointContainedWithinHorizontalThreshold(point: CGPoint) -> Bool {
return CGRectContainsPoint(CGRectMake(0, 0, horizontalThreshold, view.bounds.height), point)
private func isLeftPointContainedWithinRect(point: CGPoint) -> Bool {
return CGRectContainsPoint(CGRectMake(0, 0, horizontalThreshold, view.frame.height), point)
}
//
......@@ -407,27 +411,28 @@ public class SideNavigationViewController: UIViewController, UIGestureRecognizer
// :name: layoutBackdropLayer
//
private func layoutBackdropLayer() {
MaterialAnimation.animationDisabled {
MaterialAnimation.animationDisabled({
self.backdropLayer.frame = self.view.bounds
self.backdropLayer.zPosition = 900
self.backdropLayer.hidden = true
}
})
}
//
// :name: prepareViewControllerWithinContainer
//
private func prepareViewControllerWithinContainer(controller: UIViewController, container: UIView) {
controller.view.clipsToBounds = true
addChildViewController(controller)
container.addSubview(controller.view)
controller.didMoveToParentViewController(self)
}
//
// :name: prepareLeftGestures
// :name: prepareGestures
//
private func prepareLeftGestures() {
private func prepareGestures() {
removeGestures(&sidePanGesture, tap: &sideTapGesture)
prepareGestures(&sidePanGesture, panSelector: "handlePanGesture:", tap: &sideTapGesture, tapSelector: "handleTapGesture:")
prepareGestures(&sidePanGesture, panSelector: "handleLeftPanGesture:", tap: &sideTapGesture, tapSelector: "handleLeftTapGesture:")
}
}
\ No newline at end of file
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