Skip to content

Instantly share code, notes, and snippets.

@akisute
Created December 26, 2014 07:21
Show Gist options
  • Save akisute/4d0127fa8b586556b21f to your computer and use it in GitHub Desktop.
Save akisute/4d0127fa8b586556b21f to your computer and use it in GitHub Desktop.
- (void)addViewController:(BOOL)animated
{
CGRect destFinalFrame = self.destinationFrame;
CGRect destInitialFrame = CGRectMake(CGRectGetMaxX(destFinalFrame),
CGRectGetMinY(destFinalFrame),
CGRectGetWidth(destFinalFrame),
CGRectGetHeight(destFinalFrame));
UIViewController *srcViewController = self.sourceViewController;
UIViewController *destViewController = self.destinationViewController;
// 1. addChildViewController:
[srcViewController addChildViewController:destViewController];
// 2. didMoveToParentViewController:
[destViewController didMoveToParentViewController:srcViewController];
// 3. viewの設定
destViewController.view.frame = destInitialFrame;
destViewController.view.autoresizingMask = UIViewAutoresizingNone;
// 4. beginAppearanceTransition:animated:
[srcViewController beginAppearanceTransition:NO animated:animated];
[destViewController beginAppearanceTransition:YES animated:animated];
// 5. addSubview:
[srcViewController.view addSubview:destViewController.view];
// 6. viewのアニメーション
void (^before)() = ^{
// any pre-animation process
};
void (^block)() = ^{
// animation block
destViewController.view.frame = destFinalFrame;
};
void (^finish)() = ^{
// 7. endAppearanceTransition
[srcViewController endAppearanceTransition];
[destViewController endAppearanceTransition];
};
if (animated) {
before();
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[UIView animateWithDuration:0.3 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