Skip to content

Instantly share code, notes, and snippets.

@braking
Created May 23, 2013 11:58
Show Gist options
  • Save braking/5635569 to your computer and use it in GitHub Desktop.
Save braking/5635569 to your computer and use it in GitHub Desktop.
A custom UITabBar with a big unique center button that animates 3 buttons out of the center from any view.
#import "CustomTabBarViewController.h"
@interface CustomTabBarViewController ()
@property (nonatomic, assign) BOOL buttonsExpanded;
@end
@implementation CustomTabBarViewController
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
if (heightDifference < 0)
button.center = self.tabBar.center;
else
{
CGPoint center = self.tabBar.center;
center.y = center.y - heightDifference/2.0;
button.center = center;
}
[self.view addSubview:button];
[button addTarget:self action:@selector(centerTouched) forControlEvents:UIControlEventTouchUpInside];
[self.view bringSubviewToFront:button];
}
- (void)viewWillAppear:(BOOL)animated
{
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.backgroundColor = [UIColor redColor];
button2.backgroundColor = [UIColor blueColor];
button3.backgroundColor = [UIColor yellowColor];
button1.frame = CGRectMake(self.tabBar.center.x, self.tabBar.center.y, 0.0, 0.0);
button2.frame = CGRectMake(self.tabBar.center.x, self.tabBar.center.y, 0.0, 0.0);
button3.frame = CGRectMake(self.tabBar.center.x, self.tabBar.center.y, 0.0, 0.0);
[button1 addTarget:self action:@selector(centerTouched) forControlEvents:UIControlEventTouchUpInside];
[button2 addTarget:self action:@selector(centerTouched) forControlEvents:UIControlEventTouchUpInside];
[button3 addTarget:self action:@selector(centerTouched) forControlEvents:UIControlEventTouchUpInside];
button1.tag = 101;
button2.tag = 102;
button3.tag = 103;
[self.view addSubview:button1];
[self.view addSubview:button2];
[self.view addSubview:button3];
[self.view bringSubviewToFront:self.tabBar];
[self addCenterButtonWithImage:[UIImage imageNamed:@"cameraTabBarItem.png"] highlightImage:nil];
}
- (void)centerTouched
{
NSLog(@"Center Touched");
if (!self.buttonsExpanded) {
float screenWidth = self.view.frame.size.width;
float screenHeight = self.view.frame.size.height;
[UIView animateWithDuration:0.20 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
[[self.view viewWithTag:101] setFrame:CGRectMake((screenWidth/3 - 66.0), (screenHeight - 130), 66.0, 66.0)];
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.10 animations:^{
[[self.view viewWithTag:101] setFrame:CGRectMake((screenWidth/3 - 22.0), (screenHeight - 100), 44.0, 44.0)];
}];
}];
[UIView animateWithDuration:0.20 delay:0.05 options:UIViewAnimationOptionCurveLinear animations:^{
[[self.view viewWithTag:102] setFrame:CGRectMake((screenWidth/2 - 33.0), (screenHeight - 180), 66.0, 66.0)];
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.10 animations:^{
[[self.view viewWithTag:102] setFrame:CGRectMake((screenWidth/2 - 22.0), (screenHeight - 150), 44.0, 44.0)];
}];
}];
[UIView animateWithDuration:0.20 delay:0.10 options:UIViewAnimationOptionCurveLinear animations:^{
[[self.view viewWithTag:103] setFrame:CGRectMake((2*screenWidth/3 - 0.0), (screenHeight - 130), 66.0, 66.0)];
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.10 animations:^{
[[self.view viewWithTag:103] setFrame:CGRectMake((2*screenWidth/3 - 22.0), (screenHeight - 100), 44.0, 44.0)];
}];
}];
self.buttonsExpanded = YES;
} else {
[UIView animateWithDuration:0.25 animations:^{
[[self.view viewWithTag:101] setFrame:CGRectMake(self.tabBar.center.x, self.tabBar.center.y, 0.0, 0.0)];
[[self.view viewWithTag:102] setFrame:CGRectMake(self.tabBar.center.x, self.tabBar.center.y, 0.0, 0.0)];
[[self.view viewWithTag:103] setFrame:CGRectMake(self.tabBar.center.x, self.tabBar.center.y, 0.0, 0.0)];
} completion:^(BOOL finished) {
self.buttonsExpanded = NO;
}];
}
}
@braking
Copy link
Author

braking commented May 23, 2013

Note:
The buttons being created in viewDidLoad could be created in the storyboard and connected with an IBOutlet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment