Thanks for this great tutorial. My question is why we want to destroy the left and right panel everytime we dismiss them. Is it better just hide them. Most of the menu is state information which wonât change much.
I made some changes, but not able to uploaded.
Here are the changes on ContainerViewController
class ContainerViewController: UIViewController {
let centerPanelExpandedOffset: CGFloat = 60.0
var centerNavigationControl : UINavigationController!
var centerViewController: CenterViewController!
var currentState: SlideOutState = .BothCollapsed {
didSet {
let shouldShowShadow = currentState != .BothCollapsed
showShadowForCenterViewController(shouldShowShadow: shouldShowShadow)
}
}
lazy var leftViewController : SidePanelViewController = {
var leftController = UIStoryboard.leftViewController()
leftController?.animals = Animal.allCats()
leftController?.delegate = self.centerViewController
var frame = leftController!.view.frame
leftController?.view.frame = CGRect(x: -frame.size.width, y: frame.origin.y, width: frame.size.width - self.centerPanelExpandedOffset, height: frame.size.height)
leftController?.view.isHidden = true
self.view.addSubview(leftController!.view)
self.addChildViewController(leftController!)
leftController?.didMove(toParentViewController: self)
return leftController!
}()
lazy var rightViewController: SidePanelViewController = {
var rightController = UIStoryboard.rightViewController()
rightController?.animals = Animal.allDogs()
rightController?.delegate = self.centerViewController
var frame = rightController!.view.frame
rightController?.view.frame = CGRect(x: frame.size.width, y: frame.origin.y, width: frame.size.width - self.centerPanelExpandedOffset, height: frame.size.height)
rightController?.view.isHidden = true
self.view.addSubview(rightController!.view)
self.addChildViewController(rightController!)
rightController?.didMove(toParentViewController: self)
return rightController!
}()
override func viewDidLoad() {
super.viewDidLoad()
centerViewController = UIStoryboard.centerViewController()
centerViewController.delegate = self
centerNavigationControl = UINavigationController(rootViewController: centerViewController)
view.addSubview(centerNavigationControl.view)
addChildViewController(centerNavigationControl)
centerNavigationControl.didMove(toParentViewController: self)
//let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture(recognizer:)))
//centerNavigationControl.view.addGestureRecognizer(panGestureRecognizer)
}
}
extension ContainerViewController: CenterViewControllerDelegate {
func collapseSidePanels() {
switch (currentState) {
case .RightPanelExpanded:
currentState = .BothCollapsed
animateRightPanel(shouldExpand: false)
case .LeftPanelExpanded:
currentState = .BothCollapsed
animateLeftPanel(shouldExpand: false)
default:
break
}
}
func toggleLeftPanel() {
switch currentState {
case .BothCollapsed:
animateLeftPanel(shouldExpand: true)
case .RightPanelExpanded:
animateRightPanel(shouldExpand: false)
animateLeftPanel(shouldExpand: true)
case .LeftPanelExpanded:
break
}
}
func toggleRightPanel() {
switch currentState {
case .BothCollapsed:
animateRightPanel(shouldExpand: true)
case .LeftPanelExpanded:
animateLeftPanel(shouldExpand: false)
animateRightPanel(shouldExpand: true)
case .RightPanelExpanded:
break
}
}
func animateLeftPanel(shouldExpand: Bool) {
if (shouldExpand) {
currentState = .LeftPanelExpanded
self.leftViewController.view.isHidden = false
animatePanelXPosition(targetView: self.leftViewController.view, targetPosition: 0)
} else {
animatePanelXPosition(targetView: self.leftViewController.view, targetPosition: -self.leftViewController.view.frame.size.width){
finished in self.leftViewController.view.isHidden = true}
}
}
func animateRightPanel(shouldExpand: Bool) {
if (shouldExpand) {
currentState = .RightPanelExpanded
self.rightViewController.view.isHidden = false
animatePanelXPosition(targetView: self.rightViewController.view, targetPosition: centerPanelExpandedOffset)
} else {
animatePanelXPosition(targetView: self.rightViewController.view, targetPosition: +self.rightViewController.view.frame.size.width){
finished in self.rightViewController.view.isHidden = true}
}
}
func animatePanelXPosition(targetView: UIView, targetPosition: CGFloat, completion: ((Bool) -> Void)! = nil) {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {
targetView.frame.origin.x = targetPosition
}, completion: completion)
}
func showShadowForCenterViewController(shouldShowShadow: Bool) {
if (shouldShowShadow) {
centerNavigationControl.view.layer.shadowOpacity = 0.6
} else {
centerNavigationControl.view.layer.shadowOpacity = 0.0
}
}
}
private extension UIStoryboard {
class func mainStoryboard() â UIStoryboard { return UIStoryboard(name: âMainâ, bundle: Bundle.main) }
class func leftViewController() â SidePanelViewController? {
return mainStoryboard().instantiateViewController(withIdentifier: âLeftViewControllerâ) as? SidePanelViewController
}
class func rightViewController() â SidePanelViewController? {
return mainStoryboard().instantiateViewController(withIdentifier: âRightViewControllerâ) as? SidePanelViewController
}
class func centerViewController() â CenterViewController? {
return mainStoryboard().instantiateViewController(withIdentifier: âCenterViewControllerâ) as? CenterViewController
}