Skip to content

Commit

Permalink
Added MoveAndScale-Animation
Browse files Browse the repository at this point in the history
  • Loading branch information
myell0w committed Feb 17, 2012
1 parent 391d693 commit 10acd37
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(

NGVerticalTabBarController *tabBarController = [[NGVerticalTabBarController alloc] initWithDelegate:self];

tabBarController.animation = NGVerticalTabBarControllerAnimationMove;
tabBarController.animation = NGVerticalTabBarControllerAnimationMoveAndScale;
tabBarController.viewControllers = viewController;
self.window.rootViewController = tabBarController;

Expand Down
38 changes: 33 additions & 5 deletions NGVerticalTabBarController/NGVerticalTabBarController.m
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#import "NGVerticalTabBarController.h"


// the default width of the tabBar
#define kNGTabBarControllerDefaultWidth 150.f
#define kNGTabBarCellDefaultHeight 120.f
#define kNGDefaultAnimationDuration 0.3f
#define kNGTabBarControllerDefaultWidth 150.0f
#define kNGTabBarCellDefaultHeight 120.0f
#define kNGDefaultAnimationDuration 0.3f
#define kNGScaleFactor 0.98f
#define kNGScaleDuration 0.15f


@interface NGVerticalTabBarController () <UITableViewDataSource, UITableViewDelegate> {
// re-defined as mutable
Expand All @@ -16,6 +20,8 @@ @interface NGVerticalTabBarController () <UITableViewDataSource, UITableViewDele
unsigned int shouldSelectViewController:1;
unsigned int didSelectViewController:1;
} _delegateFlags;

BOOL _transitionAnimationActive;
}

// re-defined as read/write
Expand All @@ -35,6 +41,7 @@ - (CGFloat)delegatedHeightOfTabBarCellAtIndex:(NSUInteger)index;

@end


@implementation NGVerticalTabBarController

@synthesize viewControllers = _viewControllers;
Expand All @@ -58,6 +65,7 @@ - (id)initWithDelegate:(id<NGVerticalTabBarControllerDelegate>)delegate {
_oldSelectedIndex = NSNotFound;
_animation = NGVerticalTabBarControllerAnimationNone;
_animationDuration = kNGDefaultAnimationDuration;
_transitionAnimationActive = NO;

// need to call setter here
self.delegate = delegate;
Expand Down Expand Up @@ -320,7 +328,7 @@ - (void)updateUI {
UIViewController *oldSelectedViewController = [self.viewControllers objectAtIndex:oldSelectedIndexPath.row];
[self.tabBar deselectRowAtIndexPath:oldSelectedIndexPath animated:NO];

if (self.containmentAPISupported) {
if (self.containmentAPISupported) {
// custom move animation
if (self.animation == NGVerticalTabBarControllerAnimationMove ||
self.animation == NGVerticalTabBarControllerAnimationMoveAndScale) {
Expand All @@ -333,11 +341,20 @@ - (void)updateUI {
}

newSelectedViewController.view.frame = frame;

if (self.animation == NGVerticalTabBarControllerAnimationMoveAndScale) {
[UIView animateWithDuration:kNGScaleDuration
animations:^{
oldSelectedViewController.view.transform = CGAffineTransformMakeScale(kNGScaleFactor, kNGScaleFactor);
newSelectedViewController.view.transform = CGAffineTransformMakeScale(kNGScaleFactor, kNGScaleFactor);
}];
}
}

// if the user switches tabs too fast the viewControllers disappear from view hierarchy
// this is a workaround to not allow the user to switch during an animated transition
self.tabBar.userInteractionEnabled = NO;
_transitionAnimationActive = YES;

[self transitionFromViewController:oldSelectedViewController
toViewController:newSelectedViewController
Expand All @@ -346,7 +363,7 @@ - (void)updateUI {
animations:^{
if (self.animation == NGVerticalTabBarControllerAnimationMove ||
self.animation == NGVerticalTabBarControllerAnimationMoveAndScale) {
CGRect frame = self.childViewControllerFrame;
CGRect frame = oldSelectedViewController.view.frame;

newSelectedViewController.view.frame = frame;

Expand All @@ -361,6 +378,17 @@ - (void)updateUI {
} completion:^(BOOL finished) {
self.tabBar.userInteractionEnabled = YES;

if (self.animation == NGVerticalTabBarControllerAnimationMoveAndScale) {
[UIView animateWithDuration:kNGScaleDuration
animations:^{
oldSelectedViewController.view.transform = CGAffineTransformMakeScale(1.f, 1.f);
newSelectedViewController.view.transform = CGAffineTransformMakeScale(1.f, 1.f);
} completion:^(BOOL finished) {
newSelectedViewController.view.frame = self.childViewControllerFrame;
_transitionAnimationActive = NO;
}];
}

// call the delegate that we changed selection
[self callDelegateDidSelectViewController:newSelectedViewController atIndex:self.selectedIndex];
}];
Expand Down

0 comments on commit 10acd37

Please sign in to comment.