Skip to content

Instantly share code, notes, and snippets.

@sundeepgupta
Last active August 29, 2015 14:12
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 sundeepgupta/036044c3fd7b2b1e5f18 to your computer and use it in GitHub Desktop.
Save sundeepgupta/036044c3fd7b2b1e5f18 to your computer and use it in GitHub Desktop.
Zoom modal custom animator for iOS
#import "CCCardDetailTransitionAnimator.h"
- (void)prepareForCardDetailSequeWithCard:(CCCard *)card navigationController:(UINavigationController *)navigationController {
CCCardDetailViewController *destinationVc = (CCCardDetailViewController *)navigationController.viewControllers.firstObject;
[destinationVc configureWithCard:card delegate:self];
navigationController.modalPresentationStyle = UIModalPresentationCustom;
navigationController.transitioningDelegate = self;
}
#pragma mark - UIViewControllerTransitioningDelegate
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
[self.animationController shouldPresent];
return self.animationController;
}
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
[self.animationController shouldDismiss];
return self.animationController;
}
#import <Foundation/Foundation.h>
@interface CCCardDetailTransitionAnimator : NSObject <UIViewControllerAnimatedTransitioning>
- (void)shouldPresent;
- (void)shouldDismiss;
- (void)configureDuration:(NSTimeInterval)duration;
@end
#import "CCCardDetailTransitionAnimator.h"
@interface CCCardDetailTransitionAnimator ()
@property BOOL isPresenting;
@property NSTimeInterval duration;
@end
@implementation CCCardDetailTransitionAnimator
- (instancetype)init {
self = [super init];
if (self) {
self.isPresenting = YES;
self.duration = 0.35;
}
return self;
}
- (void)shouldPresent {
self.isPresenting = YES;
}
- (void)shouldDismiss {
self.isPresenting = NO;
}
- (void)configureDuration:(NSTimeInterval)duration {
self.duration = duration;
}
#pragma mark - UIViewControllerAnimatedTransitioning
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
return self.duration;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
UIViewController *fromVc = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toVc = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *fromView = fromVc.view;
UIView *toView = toVc.view;
UIView *containerView = transitionContext.containerView;
NSTimeInterval duration = [self transitionDuration:transitionContext];
if (self.isPresenting) {
UIViewController *topFromVc = [self topViewControllerForViewController:fromVc];
[topFromVc viewWillDisappear:YES];
fromView.userInteractionEnabled = NO;
[containerView addSubview:toView];
toView.transform = CGAffineTransformMakeScale(0.0, 0.0);
[UIView animateWithDuration:duration animations: ^{
toView.transform = CGAffineTransformMakeScale(0.9, 0.9);
fromView.alpha = 0.5;
} completion: ^(BOOL finished) {
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
[topFromVc viewDidDisappear:YES];
}];
} else {
UIViewController *topToVc = [self topViewControllerForViewController:toVc];
[topToVc viewWillAppear:YES];
[UIView animateWithDuration:duration animations: ^{
fromView.transform = CGAffineTransformMakeScale(0.01, 0.01);
toView.alpha = 1.0;
} completion: ^(BOOL finished) {
[fromView removeFromSuperview];
toView.userInteractionEnabled = YES;
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
[topToVc viewDidAppear:YES];
}];
}
}
#pragma mark - Private
- (UIViewController *)topViewControllerForViewController:(UIViewController *)viewController {
if ([viewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *nc = (UINavigationController *)viewController;
return nc.topViewController;
} else {
return viewController;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment