Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CustomTabBar: Bug fixing... #7

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions CustomTabBar/Classes/CustomTabBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@

- (UIImage*) imageFor:(CustomTabBar*)tabBar atIndex:(NSUInteger)itemIndex;
- (UIImage*) backgroundImage;
- (UIImage*) selectedItemBackgroundImage;
- (UIImage*) glowImage;
- (UIImage*) selectedItemBackgroundImageWith:(NSInteger)index;
- (UIImage*) glowImageWith:(NSInteger)index;
- (UIImage*) selectedItemImage;
- (UIImage*) tabBarArrowImage;

Expand All @@ -53,7 +53,7 @@

- (void) selectItemAtIndex:(NSInteger)index;
- (void) glowItemAtIndex:(NSInteger)index;
- (void) removeGlowAtIndex:(NSInteger)index;
- (void) hideGlow;

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;

Expand Down
85 changes: 54 additions & 31 deletions CustomTabBar/Classes/CustomTabBar.m
Original file line number Diff line number Diff line change
Expand Up @@ -165,37 +165,60 @@ - (void) selectItemAtIndex:(NSInteger)index
[self dimAllButtonsExcept:button];
}

// Add a glow at the bottom of the specified item
- (void) glowItemAtIndex:(NSInteger)index
{
// Get the right button. We'll use to calculate where to put the glow
UIButton* button = [buttons objectAtIndex:index];

// Ask the delegate for the glow image
UIImage* glowImage = [delegate glowImage];

// Create the image view that will hold the glow image
UIImageView* glowImageView = [[[UIImageView alloc] initWithImage:glowImage] autorelease];

// Center the glow image at the center of the button horizontally and at the bottom of the button vertically
glowImageView.frame = CGRectMake(button.frame.size.width/2.0 - glowImage.size.width/2.0, button.frame.origin.y + button.frame.size.height - glowImage.size.height, glowImage.size.width, glowImage.size.height);

// Set the glow image view's tag so we can find it later when we want to remove the glow
glowImageView.tag = GLOW_IMAGE_TAG;

// Add the glow image view to the button
[button addSubview:glowImageView];
- (void) hideGlow {
UIImageView* oldGlowImageView = (UIImageView*)[self viewWithTag:GLOW_IMAGE_TAG];
[UIView animateWithDuration:0.2 animations:^(void) {
oldGlowImageView.alpha = 0;
}];
}

// Remove the glow at the bottom of the specified item
- (void) removeGlowAtIndex:(NSInteger)index
{
// Find the right button
UIButton* button = [buttons objectAtIndex:index];
// Find the glow image view
UIImageView* glowImageView = (UIImageView*)[button viewWithTag:GLOW_IMAGE_TAG];
// Remove it from the button
[glowImageView removeFromSuperview];
- (void) glowItemAtIndex:(NSInteger)index {
// Get the right button. We'll use to calculate where to put the glow
UIButton* button = [buttons objectAtIndex:index];

// Ask the delegate for the glow image
UIImage* glowImage = [delegate glowImageWith:(NSInteger)index];

// Create the image view that will hold the glow image
UIImageView* newGlowImageView = [[[UIImageView alloc] initWithImage:glowImage] autorelease];
// Center the glow image at the center of the button horizontally and at the bottom of the button vertically
newGlowImageView.frame = CGRectMake(button.frame.size.width/2.0 - glowImage.size.width/2.0, button.frame.origin.y + button.frame.size.height - glowImage.size.height, glowImage.size.width, glowImage.size.height);
newGlowImageView.alpha = 0;

[button addSubview:newGlowImageView];

UIImageView* oldGlowImageView = (UIImageView*)[self viewWithTag:GLOW_IMAGE_TAG];
newGlowImageView.tag = GLOW_IMAGE_TAG;

if (oldGlowImageView) {
// oldGlowImageView already start hiding with animation
if (oldGlowImageView.alpha != 0) {
[UIView animateWithDuration:0.05 animations:^(void) {
oldGlowImageView.alpha = 0;
newGlowImageView.alpha = 0.3;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.15 animations:^(void) {
newGlowImageView.alpha = 1.0;
} completion:^(BOOL finished) {
[oldGlowImageView removeFromSuperview];
}];
}];
} else {
[UIView animateWithDuration:0.1 animations:^(void) {
newGlowImageView.alpha = 1.0;
} completion:^(BOOL finished) {
[oldGlowImageView removeFromSuperview];
}];
}
} else {
[UIView animateWithDuration:0.05 animations:^(void) {
newGlowImageView.alpha = 0.3;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.1 animations:^(void) {
newGlowImageView.alpha = 1.0;
}];
}];
}
}

- (CGFloat) horizontalLocationFor:(NSUInteger)tabIndex
Expand All @@ -220,7 +243,7 @@ - (void) addTabBarArrowAtIndex:(NSUInteger)itemIndex
tabBarArrow.tag = TAB_ARROW_IMAGE_TAG;
// To get the vertical location we go up by the height of arrow and then come back down 2 pixels so the arrow is slightly on top of the tab bar.
CGFloat verticalLocation = -tabBarArrowImage.size.height + 2;
tabBarArrow.frame = CGRectMake([self horizontalLocationFor:itemIndex], verticalLocation, tabBarArrowImage.size.width, tabBarArrowImage.size.height);
tabBarArrow.frame = CGRectMake([self horizontalLocationFor:itemIndex] - 5, verticalLocation, tabBarArrowImage.size.width, tabBarArrowImage.size.height);

[self addSubview:tabBarArrow];
}
Expand All @@ -237,7 +260,7 @@ - (UIButton*) buttonAtIndex:(NSUInteger)itemIndex width:(CGFloat)width
// Create the normal state image by converting the image's background to gray
UIImage* buttonImage = [self tabBarImage:rawButtonImage size:button.frame.size backgroundImage:nil];
// And create the pressed state image by converting the image's background to the background image we get from the delegate
UIImage* buttonPressedImage = [self tabBarImage:rawButtonImage size:button.frame.size backgroundImage:[delegate selectedItemBackgroundImage]];
UIImage* buttonPressedImage = [self tabBarImage:rawButtonImage size:button.frame.size backgroundImage:[delegate selectedItemBackgroundImageWith:itemIndex]];

// Set the gray & blue images as the button states
[button setImage:buttonImage forState:UIControlStateNormal];
Expand Down
2 changes: 1 addition & 1 deletion CustomTabBar/Classes/CustomTabBarAppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet CustomTabBarViewController *viewController;
@property (nonatomic, retain) CustomTabBarViewController *viewController;

@end

24 changes: 20 additions & 4 deletions CustomTabBar/Classes/CustomTabBarAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,28 @@ @implementation CustomTabBarAppDelegate
#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIViewController *detailController1 = [[[UIViewController alloc] init] autorelease];
detailController1.view.backgroundColor = [UIColor redColor];

// Override point for customization after application launch.
UIViewController *detailController2 = [[[UIViewController alloc] init] autorelease];
detailController2.view.backgroundColor = [UIColor greenColor];

UIViewController *detailController3 = [[[UIViewController alloc] init] autorelease];
detailController3.view.backgroundColor = [UIColor blueColor];

UIViewController *detailController4 = [[[UIViewController alloc] init] autorelease];
detailController4.view.backgroundColor = [UIColor cyanColor];

UIViewController *detailController5 = [[[UIViewController alloc] init] autorelease];
detailController5.view.backgroundColor = [UIColor purpleColor];

CustomTabBarViewController* customTabBarViewController = [[CustomTabBarViewController alloc] initWihViewControllers:[NSArray arrayWithObjects:detailController1, detailController2, detailController3, detailController4, detailController5, nil]
imagesNames:[NSArray arrayWithObjects:@"chat.png", @"compose-at.png", @"messages.png", @"magnifying-glass.png", @"more.png", nil]];
self.viewController = customTabBarViewController;
[customTabBarViewController release];

// Add the view controller's view to the window and display.
[self.window addSubview:viewController.view];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];

return YES;
Expand Down
9 changes: 6 additions & 3 deletions CustomTabBar/Classes/CustomTabBarViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@

#import "CustomTabBar.h"

@interface CustomTabBarViewController : UIViewController <CustomTabBarDelegate>
{
CustomTabBar* tabBar;
@interface CustomTabBarViewController : UIViewController <CustomTabBarDelegate> {
CustomTabBar* tabBar;
NSTimer* glowTimer;
}

- (id)initWihViewControllers:(NSArray*)viewControllers imagesNames:(NSArray*)imagesNames;

@property (nonatomic, assign) NSInteger currentIndex;
@property (nonatomic, retain) CustomTabBar* tabBar;

@end
Expand Down
Loading