Skip to content

Instantly share code, notes, and snippets.

@akisute
Created December 26, 2014 07:35
Show Gist options
  • Save akisute/e1b6176fab25ba34a2df to your computer and use it in GitHub Desktop.
Save akisute/e1b6176fab25ba34a2df to your computer and use it in GitHub Desktop.
- (void)removeViewController:(BOOL)animated
{
CGRect destInitialFrame = self.destinationFrame;
CGRect destFinalFrame = CGRectMake(CGRectGetMaxX(destInitialFrame),
CGRectGetMinY(destInitialFrame),
CGRectGetWidth(destInitialFrame),
CGRectGetHeight(destInitialFrame));
UIViewController *srcViewController = self.sourceViewController;
UIViewController *destViewController = self.destinationViewController;
// 1. beginAppearanceTransition:animated:
[srcViewController beginAppearanceTransition:YES animated:animated];
[destViewController beginAppearanceTransition:NO animated:animated];
// 2. viewのアニメーション
void (^before)() = ^{
// any pre-animation process
};
void (^block)() = ^{
// animation block
destViewController.view.frame = destFinalFrame;
};
void (^finish)() = ^{
// 3. removeFromSuperview
[destViewController.view removeFromSuperview];
// 4. endAppearanceTransition
[destViewController endAppearanceTransition];
[srcViewController endAppearanceTransition];
// 5. willMoveToParentViewController:
[destViewController willMoveToParentViewController:nil];
// 6. removeFromParentViewController
[destViewController removeFromParentViewController];
};
if (animated) {
before();
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[UIView animateWithDuration:self.animationDuration delay:0 options:UIViewAnimationOptionCurveEaseOut animations:block completion:^(BOOL finished) {
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
finish();
}];
} else {
before();
block();
finish();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment