Created
October 3, 2013 01:59
-
-
Save benjaminjackson/6803521 to your computer and use it in GitHub Desktop.
UIViewControllerAnimatedTransitioning Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// LFSlideTransitionAnimator.m | |
// Longform | |
// | |
// Created by Benjamin Jackson on 9/16/13. | |
// Copyright (c) 2013 Longform. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
typedef enum _LFSlideTransitionDirection { | |
LFSlideTransitionDirectionLeft, | |
LFSlideTransitionDirectionRight | |
} LFSlideTransitionDirection; | |
@interface LFSlideTransitionAnimator : NSObject <UIViewControllerAnimatedTransitioning> | |
@property (nonatomic) CGFloat viewWidth; | |
@property (nonatomic) BOOL isPresenting; | |
@property (nonatomic) LFSlideTransitionDirection transitionDirection; | |
@end | |
static const NSTimeInterval AnimationDuration = 0.5; | |
static const CGFloat DefaultViewWidth = 320; | |
#import "LFSlideTransitionAnimator.h" | |
@implementation LFSlideTransitionAnimator | |
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext { | |
return AnimationDuration; | |
} | |
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext { | |
if (!self.viewWidth) { | |
self.viewWidth = DefaultViewWidth; | |
} | |
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; | |
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; | |
CGFloat startX = self.transitionDirection == LFSlideTransitionDirectionLeft ? -1 * self.viewWidth : CGRectGetWidth(fromVC.view.bounds); | |
CGRect startFrame = CGRectMake(startX, 0, self.viewWidth, fromVC.view.bounds.size.height); | |
CGRect transformedStartFrame = [[transitionContext containerView] convertRect:startFrame fromView:fromVC.view]; | |
CGFloat endX = self.transitionDirection == LFSlideTransitionDirectionLeft ? 0 : CGRectGetWidth(fromVC.view.bounds) - self.viewWidth; | |
CGRect endFrame = CGRectMake(endX, 0, self.viewWidth, fromVC.view.bounds.size.height); | |
CGRect transformedEndFrame = [[transitionContext containerView] convertRect:endFrame fromView:fromVC.view]; | |
if (self.isPresenting) { | |
[transitionContext.containerView addSubview:fromVC.view]; | |
[transitionContext.containerView addSubview:toVC.view]; | |
toVC.view.frame = transformedStartFrame; | |
[UIView animateWithDuration:AnimationDuration | |
delay:0 | |
usingSpringWithDamping:0.8 | |
initialSpringVelocity:0.5 | |
options:UIViewAnimationOptionCurveEaseOut | |
animations:^{ | |
toVC.view.frame = transformedEndFrame; | |
} completion:^(BOOL finished) { | |
[transitionContext completeTransition:YES]; | |
}]; | |
} | |
else { | |
[transitionContext.containerView addSubview:toVC.view]; | |
[transitionContext.containerView addSubview:fromVC.view]; | |
[UIView animateWithDuration:AnimationDuration | |
delay:0 | |
usingSpringWithDamping:0.8 | |
initialSpringVelocity:0.5 | |
options:UIViewAnimationOptionCurveEaseOut | |
animations:^{ | |
fromVC.view.frame = transformedStartFrame; | |
} completion:^(BOOL finished) { | |
[transitionContext completeTransition:YES]; | |
}]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment