Slide out effect

12월 마지막 주, 크리스마스를 스위프트와 함께… ✨ 오늘은 RayWenderlich의 Slide out navigation panel 튜토리얼을 따라해보는 시간을 가졌습니다.

Slide Out Flow

view.addSubview(centerNavigationController.view)
addChildViewController(centerNavigationController)
centerNavigationController.didMove(toParentViewController: self)
enum SlideState {
	case leftExpanded // 왼쪽 메뉴만 열린 경우
	case rightExpanded // 오른쪽 메뉴만 열린 경우
	case bothCollapsed // 모두 닫힌 경우 (default)
	
}
var currentState: SlideOutState = .bothCollapsed

func toggleLeftPanel()

func toggleLeftPanel() {
	let shouldExpand: Bool = currentState != .leftExpanded
	if shouldExpand {
	  addLeftPanelViewController()
	}
	animateLeftPanel(shouldExpand: shouldExpand)
}

func animateCenterPanelXPosition()

func animateCenterPanelXPosition(
    targetPosition: CGFloat,
    completion: ((Bool)->Void)? = nil) {
    
    UIView.animate(withDuration: 1,
                   delay: 0,
                   usingSpringWithDamping: 0.8,
                   initialSpringVelocity: 0,
                   options: .curveEaseIn,
                   animations: {
                    self.centerNavigationController.view.frame.origin.x = targetPosition
    }, completion: completion)
}

func animateLeftPanel()

// let centerOffset: CGFloat = 60
func animateLeftPanel(shouldExpand: Bool) {
	if shouldExpand {
	  currentState = .leftExpanded
	  animateCenterPanelXPosition(
	    targetPosition: centerViewController.view.frame.width - centerOffset
	  )
	}else {
	  animateCenterPanelXPosition(
	    targetPosition: 0,
	    completion: { (finished) in
	      self.currentState = .bothCollapsed
	      self.leftViewController?.view.removeFromSuperview()
	      self.leftViewController = nil
	  })
	}
}