Created
March 5, 2015 11:22
-
-
Save benvium/4c84ef11b043e3d8eafc to your computer and use it in GitHub Desktop.
UITabBarController slide animation. Slide left and right when tabs are selected. Based on code from StackOverflow (linked in code)
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
- (BOOL) tabBarController:(UITabBarController *)tabBarController | |
shouldSelectViewController:(UIViewController *)viewController { | |
// http://stackoverflow.com/questions/5161730/iphone-how-to-switch-tabs-with-an-animation | |
NSUInteger controllerIndex = [self.viewControllers indexOfObject:viewController]; | |
if (controllerIndex == tabBarController.selectedIndex) { | |
return NO; | |
} | |
// Get the views. | |
UIView *fromView = tabBarController.selectedViewController.view; | |
UIView *toView = [tabBarController.viewControllers[controllerIndex] view]; | |
// Get the size of the view area. | |
CGRect viewSize = fromView.frame; | |
BOOL scrollRight = controllerIndex > tabBarController.selectedIndex; | |
// Add the to view to the tab bar view. | |
[fromView.superview addSubview:toView]; | |
// Position it off screen. | |
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width; | |
toView.frame = CGRectMake((scrollRight ? screenWidth : -screenWidth), viewSize.origin.y, screenWidth, viewSize.size.height); | |
[UIView animateWithDuration:0.2 | |
delay:0 | |
options:UIViewAnimationOptionCurveEaseInOut | |
animations:^{ | |
// Animate the views on and off the screen. This will appear to slide. | |
fromView.frame = CGRectMake((scrollRight ? -screenWidth : screenWidth), viewSize.origin.y, screenWidth, viewSize.size.height); | |
toView.frame = CGRectMake(0, viewSize.origin.y, screenWidth, viewSize.size.height); | |
} | |
completion:^(BOOL finished) { | |
if (finished) { | |
// Remove the old view from the tabbar view. | |
[fromView removeFromSuperview]; | |
tabBarController.selectedIndex = controllerIndex; | |
} | |
}]; | |
return NO; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment