Skip to content

Instantly share code, notes, and snippets.

@andkon
Created June 20, 2014 19:00
Show Gist options
  • Save andkon/7aa32a71af81b57a4fbb to your computer and use it in GitHub Desktop.
Save andkon/7aa32a71af81b57a4fbb to your computer and use it in GitHub Desktop.
Custom dismissal animation
@interface RootVC : UIViewController <UIViewControllerTransitioningDelegate>
#import "TwoStepTransition.h"
- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
TwoStepTransition *twoStep = [[TwoStepTransition alloc] init];
return twoStep;
}
#import <Foundation/Foundation.h>
@interface TwoStepTransition : NSObject <UIViewControllerAnimatedTransitioning>
@end
@implementation TwoStepTransition
// Just like w/ BouncyTransition
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return 1.0f;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
// 1. Get the to and from VCs (they're flipped compared to the presentingModal transition)
UIViewController *toVC = [transitionContext viewControllerForKey:UIViewControllerTransitionContextToViewControllerKey];
UIViewContorller *fromVC = [transitionContext viewControllerForKey:UIViewControllerTransitionContextFromViewControllerKey];
// they're also both already in the container view, so no need to add 'em
// 2. Implement keyframe animations
__block CGRect presentedFrame = [transitionContext initialFrameForViewController:fromVC];
[UIView animatedKeyframesWithDuration:1.0f
delay:0.0f
options:0
animations:^{
[UIView addKeyframeWithRelativeStartTime:0.0
relativeDuration:0.8
animations:^{
fromVC.view.frame = CGRectMake(
presentedFrame.origin.x,
-20,
presentedFrame.size.width,
presentedFrame.size.height
);
}];
[UIView addKeyframeWithRelativeStartTime:0.8
relativeDuration:0.2
animations:^{
presentedFrame.origin.y += CGRectGetHeight(presentedFrame) + 20;
fromVC.view.frame = presentedFrame;
fromVC.view.transform = CGAffineTransformMakeRotation(0.2);
}];
} completed:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment