Skip to content

Commit

Permalink
Merge pull request #15 from vokalinteractive/master
Browse files Browse the repository at this point in the history
Added option to fade in new controller
  • Loading branch information
Josh Johnson committed Jan 3, 2014
2 parents da255fa + 245e7e9 commit 283652f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
8 changes: 8 additions & 0 deletions TWTSideMenuViewController/TWTSideMenuViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@

@interface TWTSideMenuViewController : UIViewController

typedef NS_ENUM(NSInteger, TWTSideMenuAnimationType) {
TWTSideMenuAnimationTypeSlideOver, //Default - new view controllers slide over the old view controller.
TWTSideMenuAnimationTypeFadeIn //New View controllers fade in over the old view controller.
};

/** The animation type - will default to Slide Over. */
@property (nonatomic, assign) TWTSideMenuAnimationType animationType;

/** Time interval for opening and closing the side menu */
@property (nonatomic, assign) NSTimeInterval animationDuration;

Expand Down
31 changes: 27 additions & 4 deletions TWTSideMenuViewController/TWTSideMenuViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ - (id)initWithMenuViewController:(UIViewController *)menuViewController mainView
- (void)commonInitialization
{
self.animationDuration = kDefaultAnimationDuration;
self.animationType = TWTSideMenuAnimationTypeSlideOver;

[self addViewController:self.menuViewController];
[self addViewController:self.mainViewController];
Expand Down Expand Up @@ -294,7 +295,6 @@ - (void)setMainViewController:(UIViewController *)mainViewController animated:(B
animation.duration = kDefaultAnimationDuration;
[overlayView.layer addAnimation:animation forKey:@"opacity"];

CGFloat outgoingStartX = CGRectGetMaxX(outgoingViewController.view.frame);
NSTimeInterval changeTimeInterval = kDefaultSwapAnimationDuration;
NSTimeInterval delayInterval = kDefaultAnimationDelayDuration;
if (!self.open) {
Expand All @@ -307,10 +307,33 @@ - (void)setMainViewController:(UIViewController *)mainViewController animated:(B
[self.containerView addSubview:incomingViewController.view];

incomingViewController.view.frame = self.containerView.bounds;
incomingViewController.view.transform = CGAffineTransformTranslate(incomingViewController.view.transform, outgoingStartX, 0.0f);

//Create default animation curve.
UIViewAnimationOptions options = UIViewAnimationOptionCurveEaseInOut;
switch (self.animationType) {
case TWTSideMenuAnimationTypeSlideOver: {
CGFloat outgoingStartX = CGRectGetMaxX(outgoingViewController.view.frame);

incomingViewController.view.transform = CGAffineTransformTranslate(incomingViewController.view.transform, outgoingStartX, 0.0f);
break;
}
case TWTSideMenuAnimationTypeFadeIn:
incomingViewController.view.alpha = 0.6f;
options = UIViewAnimationOptionCurveEaseOut;
break;
}


void (^swapChangeBlock)(void) = ^{
incomingViewController.view.transform = CGAffineTransformIdentity;
switch (self.animationType) {
case TWTSideMenuAnimationTypeSlideOver:
incomingViewController.view.transform = CGAffineTransformIdentity;
break;
case TWTSideMenuAnimationTypeFadeIn:
incomingViewController.view.alpha = 1.0f;
default:
break;
}
};

void (^finishedChangeBlock)(BOOL finished) = ^(BOOL finished) {
Expand All @@ -331,7 +354,7 @@ - (void)setMainViewController:(UIViewController *)mainViewController animated:(B

[UIView animateWithDuration:changeTimeInterval
delay:delayInterval
options:UIViewAnimationOptionCurveEaseInOut
options:options
animations:swapChangeBlock
completion:finishedChangeBlock];
} else {
Expand Down

0 comments on commit 283652f

Please sign in to comment.