Skip to content

Instantly share code, notes, and snippets.

@crespoxiao
Forked from kwylez/gist:5301597
Last active February 10, 2017 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save crespoxiao/a4fbd45d2368f20bb4d8 to your computer and use it in GitHub Desktop.
Save crespoxiao/a4fbd45d2368f20bb4d8 to your computer and use it in GitHub Desktop.
Custom UIViewTransition Example
- (void)transitionFromViewController:(UIViewController *)fromViewController
toViewController:(UIViewController *)toViewController
withAnimationType:(EPBUIViewAnimationTransition)animation
fromFrame:(CGRect)aFrame
withOverLayImage:(NSString *)overlayName {
static NSInteger standardImageTag = 9999;
if (fromViewController == toViewController) {
return;
}
/**
* Need to take in account the navbar
*/
toViewController.view.frame = CGRectOffset(self.view.frame, 0, kTopViewOffset);
self.selectedViewController = toViewController;
/**
* notify fromViewController
*/
[fromViewController willMoveToParentViewController:nil];
[self addChildViewController:toViewController];
/**
* Static completion animation block for inital anitmation
*
* Standard animation block that starts transition
*/
void (^vt_animation_block)(void) = ^(void){};
/**
* Animation block is triggered once the inital animation has
* completed.
*/
void (^vt_completion_animation_block)(void) = ^(void){};
/**
* Completion block for animation trigged by inital animation completion.
* I know...it is a mouth full
*/
void (^vt_completion_animation_completion_block)(BOOL) = ^(BOOL finished){};
switch (animation) {
case EPBUIViewAnimationTransitionFadeIn: {
[toViewController.view addImageSubview:overlayName
withFrame:CGRectZero
tag:standardImageTag];
[toViewController.view viewWithTag:standardImageTag].frame = aFrame;
vt_animation_block = ^{
fromViewController.view.alpha = 0.0f;
[toViewController.view viewWithTag:standardImageTag].frame = toViewController.view.bounds;
};
vt_completion_animation_block = ^{
[toViewController.view viewWithTag:standardImageTag].alpha = 0.0f;
};
vt_completion_animation_completion_block = ^(BOOL finished) {
[[toViewController.view viewWithTag:standardImageTag] removeFromSuperview];
[toViewController didMoveToParentViewController:self];
[fromViewController removeFromParentViewController];
};
break;
}
case EPBUIViewAnimationTransitionFadeOut: {
[fromViewController.view addImageSubview:overlayName
withFrame:fromViewController.view.frame
tag:standardImageTag];
vt_animation_block = ^{
toViewController.view.alpha = 1.0f;
[fromViewController.view viewWithTag:standardImageTag].frame = self.originalImageFrame;
};
vt_completion_animation_block = ^{
[fromViewController.view viewWithTag:standardImageTag].alpha = 0.0f;
};
vt_completion_animation_completion_block = ^(BOOL finished) {
[[fromViewController.view viewWithTag:standardImageTag] removeFromSuperview];
[toViewController didMoveToParentViewController:self];
[fromViewController removeFromParentViewController];
};
break;
}
default: {
_transitionType = EPBUIViewAnimationTransitionNone;
break;
}
}
/**
* transition
*/
[self transitionFromViewController:fromViewController
toViewController:toViewController
duration:0.5
options:UIViewAnimationOptionAllowAnimatedContent
animations:vt_animation_block
completion:^(BOOL finished) {
[UIView animateWithDuration:0.5
animations:vt_completion_animation_block
completion:vt_completion_animation_completion_block
];
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment