Skip to content

Commit

Permalink
Animated show/hide of tabBar working
Browse files Browse the repository at this point in the history
  • Loading branch information
myell0w committed Apr 27, 2012
1 parent 9a8eb14 commit 03da38d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
3 changes: 0 additions & 3 deletions NGTabBarController/NGTabBarController.h
Expand Up @@ -43,9 +43,6 @@
/** The designated initializer. */
- (id)initWithDelegate:(id<NGTabBarControllerDelegate>)delegate;

/** Sets the view controllers of the tab bar controller. */
- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated;

- (void)setTabBarHidden:(BOOL)tabBarHidden animated:(BOOL)animated;

@end
61 changes: 32 additions & 29 deletions NGTabBarController/NGTabBarController.m
Expand Up @@ -19,7 +19,7 @@ @interface NGTabBarController () {
unsigned int didSelectViewController:1;
} _delegateFlags;

BOOL _moveAnimationActive;
BOOL _animationActive;
}

// re-defined as read/write
Expand Down Expand Up @@ -53,6 +53,7 @@ @implementation NGTabBarController
@synthesize delegate = _delegate;
@synthesize tabBar = _tabBar;
@synthesize tabBarPosition = _tabBarPosition;
@synthesize tabBarHidden = _tabBarHidden;
@synthesize animation = _animation;
@synthesize animationDuration = _animationDuration;
@synthesize tabBarItems = _tabBarItems;
Expand All @@ -68,7 +69,7 @@ - (id)initWithDelegate:(id<NGTabBarControllerDelegate>)delegate {
_oldSelectedIndex = NSNotFound;
_animation = NGTabBarControllerAnimationNone;
_animationDuration = kNGDefaultAnimationDuration;
_moveAnimationActive = NO;
_animationActive = NO;
_tabBarPosition = kNGTabBarPositionDefault;

// need to call setter here
Expand Down Expand Up @@ -150,7 +151,9 @@ - (void)viewDidDisappear:(BOOL)animated {
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];

[self layout];
if (!_animationActive) {
[self layout];
}
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
Expand Down Expand Up @@ -223,14 +226,6 @@ - (void)setSelectedViewController:(UIViewController *)selectedViewController {
self.selectedIndex = [self.viewControllers indexOfObject:selectedViewController];
}

- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated {
if (animated) {
// TODO:
} else {
self.viewControllers = viewControllers;
}
}

- (void)setViewControllers:(NSArray *)viewControllers {
if (viewControllers != _viewControllers) {
if (self.containmentAPISupported) {
Expand Down Expand Up @@ -303,20 +298,30 @@ - (void)setTabBarItems:(NSArray *)tabBarItems {
}

- (BOOL)tabBarHidden {
return self.tabBar.hidden;
return _tabBarHidden || self.tabBar.hidden;
}

- (void)setTabBarHidden:(BOOL)tabBarHidden {
[self setTabBarHidden:tabBarHidden animated:NO];
}

- (void)setTabBarHidden:(BOOL)tabBarHidden animated:(BOOL)animated {
if (animated) {
// TODO:
} else {
self.tabBar.hidden = YES;
self.selectedViewController.view.frame = self.childViewControllerFrame;
[self layout];
if (tabBarHidden != _tabBarHidden) {
_tabBarHidden = tabBarHidden;

if (animated) {
_animationActive = YES;
[UIView animateWithDuration:kNGDefaultAnimationDuration
animations:^{
self.selectedViewController.view.frame = self.childViewControllerFrame;
} completion:^(BOOL finished) {
_animationActive = NO;
[self layout];
}];
} else {
self.selectedViewController.view.frame = self.childViewControllerFrame;
[self layout];
}
}
}

Expand Down Expand Up @@ -354,7 +359,7 @@ - (void)updateUI {
}

newSelectedViewController.view.frame = frame;
_moveAnimationActive = YES;
_animationActive = YES;

if (self.animation == NGTabBarControllerAnimationMoveAndScale) {
[UIView animateWithDuration:kNGScaleDuration
Expand Down Expand Up @@ -406,13 +411,13 @@ - (void)updateUI {
newSelectedViewController.view.transform = CGAffineTransformMakeScale(1.f, 1.f);
} completion:^(BOOL finished) {
newSelectedViewController.view.frame = self.childViewControllerFrame;
_moveAnimationActive = NO;
_animationActive = NO;

// call the delegate that we changed selection
[self callDelegateDidSelectViewController:newSelectedViewController atIndex:self.selectedIndex];
}];
} else {
_moveAnimationActive = NO;
_animationActive = NO;
// call the delegate that we changed selection
[self callDelegateDidSelectViewController:newSelectedViewController atIndex:self.selectedIndex];
}
Expand Down Expand Up @@ -449,10 +454,8 @@ - (void)updateUI {
- (void)layout {
CGRect childViewControllerFrame = self.childViewControllerFrame;

if (!_moveAnimationActive) {
for (UIViewController *viewController in self.viewControllers) {
viewController.view.frame = childViewControllerFrame;
}
for (UIViewController *viewController in self.viewControllers) {
viewController.view.frame = childViewControllerFrame;
}

[self setupTabBarForPosition:self.tabBarPosition];
Expand All @@ -463,6 +466,10 @@ - (CGRect)childViewControllerFrame {
UIEdgeInsets edgeInsets = UIEdgeInsetsZero;
CGFloat inset = [self widthOrHeightOfTabBarForPosition:self.tabBarPosition];

if (self.tabBarHidden) {
inset = 0.f;
}

switch (self.tabBarPosition) {
case NGTabBarPositionTop:
edgeInsets = UIEdgeInsetsMake(inset, 0.f, 0.f, 0.f);
Expand Down Expand Up @@ -581,10 +588,6 @@ - (void)handleItemPressed:(id)sender {
}

- (CGFloat)widthOrHeightOfTabBarForPosition:(NGTabBarPosition)position {
if (self.tabBarHidden) {
return 0.f;
}

CGFloat dimension = kNGTabBarControllerDefaultWidth;

// first item is responsible for dimension of tabBar, all must be equal (will not be checked)
Expand Down

0 comments on commit 03da38d

Please sign in to comment.