Skip to content

Instantly share code, notes, and snippets.

@pietrorea
Created May 15, 2014 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pietrorea/08f1a9c861561bcd36fd to your computer and use it in GitHub Desktop.
Save pietrorea/08f1a9c861561bcd36fd to your computer and use it in GitHub Desktop.
TPSpringyModalTransition - modal transition that bounces off the top like a spring
// TPSpringyModalTransition.h
// iPeru
//
// Created by Pietro Rea on 5/4/14.
// Copyright (c) 2014 Pietro Rea. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface TPSpringyModalTransition : NSObject <UIViewControllerAnimatedTransitioning>
@property (assign, nonatomic) BOOL presenting;
@end
//
// TPSpringyModalTransition.m
// iPeru
//
// Created by Pietro Rea on 5/4/14.
// Copyright (c) 2014 Pietro Rea. All rights reserved.
//
#import "TPSpringyModalTransition.h"
#import <FrameAccessor/FrameAccessor.h>
#define kHeightDelta 30
@implementation TPSpringyModalTransition
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
return 0.45;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
UIViewController *presentingVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *presentedVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
if (self.presenting) {
[transitionContext.containerView addSubview:presentingVC.view];
[transitionContext.containerView addSubview:presentedVC.view];
presentedVC.view.height += kHeightDelta;
presentedVC.view.top += presentedVC.view.height;
[UIView animateWithDuration:[self transitionDuration:transitionContext]
delay:0.0
usingSpringWithDamping:0.70
initialSpringVelocity:0.9
options:0
animations:^{
presentedVC.view.top = presentingVC.view.top;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
presentedVC.view.height -= kHeightDelta;
}];
} else {
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
presentingVC.view.frame = CGRectOffset(presentingVC.view.frame, 0, presentingVC.view.frame.size.height);
} completion:^(BOOL finished) {
[transitionContext.containerView addSubview:presentingVC.view];
[transitionContext.containerView addSubview:presentedVC.view];
[transitionContext completeTransition:YES];
}];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment