Skip to content

Instantly share code, notes, and snippets.

@andkon
Created June 20, 2014 03:13
Show Gist options
  • Save andkon/0137c6f86a1da02642a2 to your computer and use it in GitHub Desktop.
Save andkon/0137c6f86a1da02642a2 to your computer and use it in GitHub Desktop.
Custom Transitions Between VCs
#import <Foundation/Foundation.h>
@interface BouncyTransition : NSObject <UIViewControllerAnimatedTransitioning>
@end
@implementation BouncyTransition
- (NSTimeInterval)transitionDuration:(id<UIViewContollerContextTransitioning>)transitionContext
{
return 1.0f;
}
- (void)animateTransition:(id<UIViewContollerContextTransitioning>)transitionContext
{
// 1. Insert modal view into transition view hierarchy
// First get the to and from VCs
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
[[transitionContext containerView] addSubview:toVC.view];
// 2. Set starting position of modal view
// get initial frame of fromVC:
CGRect fullFrame = [transitionContext initialFrameForViewController:fromVC];
CGFloat height = CGRectGetHeight(fullFrame);
toView.view.frame = CGRectMake(
fullFrame.origin.x + 20,
height + 16 + 20,
CGRectGetWidth(fullFrame) - 40,
height -40
);
// 3. Animate modal into position using UIView animations
[UIView animateWithDuration:[self transitionDuration:transitionContext];
delay:0.0f
usingSpringWithDamping:0.5f
initialSpringVelocity:0.6f
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
toVC.view.frame = CGRectMake(
20,
20,
CGRectGetWidth(fullFrame) -40,
height-40);
}
// 4. Complete the transition or else the UI will be frozen
completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}
];
}
@interface RootVC : UIViewController <UIViewControllerTransitioningDelegate>
// That'll make it so you can implement the - (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController... method.
import "BouncyTransition.h"
- (void)loginButtonTapped:(id)sender
{
LoginVC *loginVC = [[LoginVC alloc] init];
loginVC.modalPresentationStyle = UIModalPresentationCustom;
loginVC.transitioningDelegate = self;
[self presentViewController:loginVC
animated:YES
completion:NIL];
}
- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
presentingViewController:(UIViewController*)presenting
sourceController:(UIViewController *)source
{
BouncyTransition *transition = [[BouncyTransition alloc] init];
return transition;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment