Skip to content

Instantly share code, notes, and snippets.

@boctor
Created January 2, 2011 17:48
Show Gist options
  • Save boctor/762676 to your computer and use it in GitHub Desktop.
Save boctor/762676 to your computer and use it in GitHub Desktop.
How does the Twitter iPhone App animate the current tab bar indicator? http://idevrecipes.com/2010/12/17/twitter-app-tab-bar-animation/#comment-96
- (void) addTabBarArrow
{
UIImage* tabBarArrowImage = [UIImage imageNamed:@"TabBarNipple.png"];
self.tabBarArrow = [[[UIImageView alloc] initWithImage:tabBarArrowImage] autorelease];
CGRect frame = CGRectMake([self horizontalLocationFor:0], -4, [tabBarArrow frame].size.width, [tabBarArrow frame].size.height);
[tabBarArrow setFrame:frame];
[[tabBarController tabBar] addSubview:tabBarArrow];
}
- (CGFloat) horizontalLocationFor:(NSUInteger)tabIndex
{
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
if (deviceOrientation == UIDeviceOrientationLandscapeRight || deviceOrientation == UIDeviceOrientationLandscapeLeft) {
NSLog(@"land");
// A single tab item's width is the entire width of the tab bar divided by number of items
CGFloat tabItemWidth = 480 / tabBarController.tabBar.items.count;
// A half width is tabItemWidth divided by 2 minus half the width of the arrow
CGFloat halfTabItemWidth = (tabItemWidth / 2.0) - (tabBarArrow.frame.size.width / 2.0);
// The horizontal location is the index times the width plus a half width
return (tabIndex * tabItemWidth) + halfTabItemWidth;
}else {
NSLog(@"Portrait");
// A single tab item's width is the entire width of the tab bar divided by number of items
CGFloat tabItemWidth = 320 / tabBarController.tabBar.items.count;
// A half width is tabItemWidth divided by 2 minus half the width of the arrow
CGFloat halfTabItemWidth = (tabItemWidth / 2.0) - (tabBarArrow.frame.size.width / 2.0);
// The horizontal location is the index times the width plus a half width
return (tabIndex * tabItemWidth) + halfTabItemWidth;
}
}
- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
CGRect frame = tabBarArrow.frame;
frame.origin.x = [self horizontalLocationFor:tabBarController.selectedIndex];
tabBarArrow.frame = frame;
[UIView commitAnimations];
}
- (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation
{
NSLog(@"oi");
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
CGRect frame = tabBarArrow.frame;
frame.origin.x = [self horizontalLocationFor:tabBarController.selectedIndex];
tabBarArrow.frame = frame;
[UIView commitAnimations];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment