Skip to content

Instantly share code, notes, and snippets.

@aciidgh
Created November 18, 2015 09:15
Show Gist options
  • Save aciidgh/e76ad7cd4e283ac1aeea to your computer and use it in GitHub Desktop.
Save aciidgh/e76ad7cd4e283ac1aeea to your computer and use it in GitHub Desktop.
Dropdown menu animation
//
// MenuCustomAnimationDelegate.h
//
//
// Created by Ankit Aggarwal on 25/05/15.
// Copyright (c) 2015 Ankit Aggarwal. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface MenuCustomAnimationDelegate : NSObject <UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate>
@property (nonatomic, strong) UIImage *snapshot;
@property (nonatomic, assign) BOOL reverse;
@end
//
// MenuCustomAnimationDelegate.m
//
//
// Created by Ankit Aggarwal on 25/05/15.
// Copyright (c) 2015 Ankit Aggarwal. All rights reserved.
//
#import "MenuCustomAnimationDelegate.h"
#import "MenuViewController.h"
@implementation MenuCustomAnimationDelegate
- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
return self;
}
- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
self.reverse = YES;
return self;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
if(self.reverse == NO)
[self animationForPresenting:transitionContext];
else
[self animationForDismissal:transitionContext];
}
- (void)animationForPresenting:(id<UIViewControllerContextTransitioning>)transitionContext {
MenuViewController *toViewController = (id)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *containerView = [transitionContext containerView];
NSTimeInterval duration = [self transitionDuration:transitionContext];
UIImageView *imageView = toViewController.imageView;
imageView.image = self.snapshot;
[containerView addSubview:toViewController.view];
imageView.transform = CGAffineTransformMakeTranslation(0, - [UIScreen mainScreen].bounds.size.width * (5.0/4));
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
[UIView animateWithDuration:duration delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0.3 options:0 animations:^{
imageView.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
[transitionContext completeTransition:!transitionContext.transitionWasCancelled];
}];
}
- (void)animationForDismissal:(id<UIViewControllerContextTransitioning>)transitionContext {
MenuViewController *fromViewController = (id)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *containerView = [transitionContext containerView];
NSTimeInterval duration = [self transitionDuration:transitionContext];
UIImageView *imageView = fromViewController.imageView;
[containerView insertSubview:toViewController.view belowSubview:fromViewController.view];
toViewController.view.alpha = 1;
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
[UIView animateWithDuration:duration delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:0.3 options:0 animations:^{
imageView.transform = CGAffineTransformMakeTranslation(0, - [UIScreen mainScreen].bounds.size.width * (5.0/4));
} completion:^(BOOL finished) {
[transitionContext completeTransition:!transitionContext.transitionWasCancelled];
}];
}
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
return self.reverse ? 0.3 : 0.3;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment