8
8

More than 5 years have passed since last update.

childViewControllerをスライドアニメーションで切り替える(ContainerViewController)

Posted at
- (void)switchToViewController:(UIViewController *)viewController transition:(SwithingTransition)transition
{
  UIViewController *toViewController = viewController;

  if (!toViewController) {
    return;
  }

  UIViewController *fromViewController = self.currentEmbededViewController;

  if (!fromViewController) {
    // 遷移前ViewControllerが存在しない場合、そのままViewに追加
    [self addChildViewController:toViewController];
    [self.view addSubview:toViewController.view];
    [toViewController didMoveToParentViewController:self];
  } else {
    CGRect toViewStartFrame = self.view.frame;
    CGRect fromViewEndFrame = self.view.frame;

    // 開始、終了フレームの設定
    switch (transition) {
      case SwitchingTransitionToRight:
        toViewStartFrame.origin.x -= self.view.frame.size.width;
        fromViewEndFrame.origin.x += self.view.frame.size.width;
        break;
      case SwitchingTransitionToLeft:
        toViewStartFrame.origin.x += self.view.frame.size.width;
        fromViewEndFrame.origin.x -= self.view.frame.size.width;
        break;
      default:
        break;
    }

    toViewController.view.alpha = 0.0;
    toViewController.view.frame = toViewStartFrame;

    [self addChildViewController:toViewController];
    // 削除開始
    [fromViewController willMoveToParentViewController:nil];

    [self transitionFromViewController:fromViewController
                      toViewController:toViewController
                              duration:1.0
                               options:UIViewAnimationOptionTransitionNone
                            animations:^{
                              fromViewController.view.frame = fromViewEndFrame;
                              toViewController.view.frame   = self.view.frame;

                              fromViewController.view.alpha = 0.0;
                              toViewController.view.alpha   = 1.0;
                            } completion:^(BOOL finished) {
                              // 追加完了
                              [toViewController didMoveToParentViewController:self];
                            }];
  }

  self.currentEmbededViewController = toViewController;
}
8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8