Skip to content

Instantly share code, notes, and snippets.

@nguyentruongky
Created November 14, 2017 10:58
Show Gist options
  • Save nguyentruongky/8a5ea81f98d17bccad548f6dc4e2855b to your computer and use it in GitHub Desktop.
Save nguyentruongky/8a5ea81f98d17bccad548f6dc4e2855b to your computer and use it in GitHub Desktop.
Slide animation in tab bar controller
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
animateSliding(fromController: selectedViewController, toController: viewController)
return false
}
func animateSliding(fromController: UIViewController?, toController: UIViewController?) {
guard let fromController = fromController, let toController = toController else { return }
guard let fromIndex = viewControllers?.index(of: fromController),
let toIndex = viewControllers?.index(of: toController) else { return }
guard fromIndex != toIndex else { return }
let fromView = fromController.view!
let toView = toController.view!
let viewSize = fromView.frame
let scrollRight = fromIndex < toIndex
fromView.superview?.addSubview(toView)
toView.frame = CGRect(x: scrollRight ? screenWidth : -screenWidth,
y: viewSize.origin.y,
width: screenWidth,
height: viewSize.height)
func animate() {
fromView.frame = CGRect(x: scrollRight ? -screenWidth : screenWidth,
y: viewSize.origin.y,
width: screenWidth,
height: viewSize.height)
toView.frame = CGRect(x: 0,
y: viewSize.origin.y,
width: screenWidth,
height: viewSize.height)
}
func finished(_ completed: Bool) {
fromView.removeFromSuperview()
selectedIndex = toIndex
}
UIView.animate(withDuration: 0.35, delay: 0, options: .curveEaseInOut,
animations: animate, completion: finished)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment