Skip to content

Instantly share code, notes, and snippets.

@pita5
Created June 18, 2013 11:16
Show Gist options
  • Save pita5/5804561 to your computer and use it in GitHub Desktop.
Save pita5/5804561 to your computer and use it in GitHub Desktop.
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
if ([self.delegate respondsToSelector:@selector(setAnimating:)])
[self.delegate setAnimating:YES];
UIView* containerView = [transitionContext containerView];
UIViewController* fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController* toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
if (fromVC && toVC)
{
if (UINavigationControllerOperationPop == self.operation)
{
CGRect frame = containerView.bounds;
frame.origin.x = frame.size.width;
toVC.view.frame = containerView.bounds;
fromVC.view.frame = containerView.bounds;
[containerView addSubview:toVC.view];
[containerView bringSubviewToFront:fromVC.view];
[containerView sendSubviewToBack:toVC.view];
UIView* contentMask = [toVC.view viewWithTag:kContentMaskKey];
if (nil == contentMask)
{
contentMask = [[UIView alloc] initWithFrame:containerView.bounds];
[toVC.view addSubview:contentMask];
}
contentMask.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.85f];
CATransform3D transform = CATransform3DIdentity;
transform.m34 = 1.0f / - 500.0f;
transform = CATransform3DTranslate(transform, 0.0f, 0.0f, -40.0f);
toVC.view.layer.transform = transform;
[UIView animateWithDuration:kTransitionDuration
delay:0.0f
options:0
animations:^{
contentMask.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0f];
[UIView addKeyframeWithRelativeStartTime:0.0
relativeDuration:kTransitionDuration/2
animations:^{
fromVC.view.frame = frame;
}];
[UIView addKeyframeWithRelativeStartTime:kTransitionDuration/2
relativeDuration:kTransitionDuration/2
animations:^{
toVC.view.layer.transform = CATransform3DIdentity;
}];
}
completion:^(BOOL finished) {
[fromVC.view removeFromSuperview];
[transitionContext completeTransition:finished];
}];
}
else if (self.operation == UINavigationControllerOperationPush)
{
CGRect frame = containerView.bounds;
frame.origin.x = frame.size.width;
toVC.view.frame = frame;
fromVC.view.frame = containerView.bounds;
[containerView addSubview:toVC.view];
[containerView bringSubviewToFront:toVC.view];
[containerView sendSubviewToBack:fromVC.view];
UIView* contentMask = [fromVC.view viewWithTag:kContentMaskKey];
if (nil == contentMask)
{
contentMask = [[UIView alloc] initWithFrame:containerView.bounds];
[fromVC.view addSubview:contentMask];
}
contentMask.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0f];
[UIView animateWithDuration:kTransitionDuration
delay:0.0f
options:0
animations:^{
contentMask.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.85f];
[UIView addKeyframeWithRelativeStartTime:0.0
relativeDuration:kTransitionDuration/2
animations:^{
toVC.view.frame = containerView.bounds;
}];
[UIView addKeyframeWithRelativeStartTime:kTransitionDuration/2
relativeDuration:kTransitionDuration/2
animations:^{
CATransform3D transform = CATransform3DIdentity;
transform.m34 = 1.0f / - 500.0f;
transform = CATransform3DTranslate(transform, 0.0f, 0.0f, -40.0f);
fromVC.view.layer.transform = transform;
}];
}
completion:^(BOOL finished) {
[fromVC.view removeFromSuperview];
[transitionContext completeTransition:finished];
}];
}
else
{
}
}
else if (toVC)
{
toVC.view.frame = containerView.bounds;
[containerView addSubview:toVC.view];
[transitionContext completeTransition:YES];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment