Skip to content

Instantly share code, notes, and snippets.

@saiday
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save saiday/4872f8d26bdd0796874a to your computer and use it in GitHub Desktop.

Select an option

Save saiday/4872f8d26bdd0796874a to your computer and use it in GitHub Desktop.
MiniToLargeViewAnimator
#import "MiniToLargeViewAnimator.h"
static NSTimeInterval kAnimationDuration = .4f;
static NSString *const kFadeoutAnimationKey = @"FadeoutAnimationKey";
static NSString *const kFadeinAnimationKey = @"FadeinAnimationKey";
@implementation MiniToLargeViewAnimator
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return kAnimationDuration;
}
- (void)animatePresentingInContext:(id<UIViewControllerContextTransitioning>)transitionContext toVC:(UIViewController *)toVC fromVC:(UIViewController *)fromVC
{
CGRect fromVCRect = [transitionContext initialFrameForViewController:fromVC];
CGRect toVCRect = fromVCRect;
toVCRect.origin.y = toVCRect.size.height - self.initialY;
toVC.view.frame = toVCRect;
UIView *container = [transitionContext containerView];
UIView *imageView = [self fakeMiniView];
[toVC.view addSubview:imageView];
[container addSubview:fromVC.view];
[container addSubview:toVC.view];
[UIView animateWithDuration:kAnimationDuration animations:^{
toVC.view.frame = fromVCRect;
imageView.alpha = 0.f;
} completion:^(BOOL finished) {
[imageView removeFromSuperview];
if ([transitionContext transitionWasCancelled]) {
[transitionContext completeTransition:NO];
} else {
[transitionContext completeTransition:YES];
}
}];
}
- (void)animateDismissingInContext:(id<UIViewControllerContextTransitioning>)transitionContext toVC:(UIViewController *)toVC fromVC:(UIViewController *)fromVC
{
CGRect fromVCRect = [transitionContext initialFrameForViewController:fromVC];
fromVCRect.origin.y = fromVCRect.size.height - self.initialY;
UIView *imageView = [self fakeMiniView];
[fromVC.view addSubview:imageView];
UIView *container = [transitionContext containerView];
[container addSubview:toVC.view];
[container addSubview:fromVC.view];
imageView.alpha = 0.f;
[UIView animateWithDuration:kAnimationDuration animations:^{
fromVC.view.frame = fromVCRect;
imageView.alpha = 1.f;
} completion:^(BOOL finished) {
[imageView removeFromSuperview];
if ([transitionContext transitionWasCancelled]) {
[transitionContext completeTransition:NO];
} else {
[transitionContext completeTransition:YES];
}
}];
}
- (UIView *)fakeMiniView
{
// Fake a mini view, two ways:
// 1. create a new certain one
// 2. snapshot old one.
return fakeView;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment