Created
November 14, 2017 10:58
-
-
Save nguyentruongky/8a5ea81f98d17bccad548f6dc4e2855b to your computer and use it in GitHub Desktop.
Slide animation in tab bar controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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