Dismiss and present modal view controller with one animation

那年仲夏 提交于 2019-12-05 11:34:57

My solution to this issue was replacing current rootViewController, supporting different transitions:

static func replaceRootViewController(with viewController: UIViewController, transition: UIViewAnimationOptions, completion: (() -> ())? = nil) {        
    if transition == .transitionCrossDissolve {
        let overlayView = UIScreen.main.snapshotView(afterScreenUpdates: false)
        viewController.view.addSubview(overlayView)
        UIApplication.shared.keyWindow?.rootViewController = viewController

        UIView.animate(withDuration: 0.65, delay: 0, options: transition, animations: {
            overlayView.alpha = 0
        }, completion: { finished in
            overlayView.removeFromSuperview()
            if let completion = completion{
                completion()
            }
        })
    } else {
        _ = viewController.view
        UIView.transition(with: UIApplication.shared.keyWindow!, duration: 0.65,options: transition, animations: {
            UIApplication.shared.keyWindow?.rootViewController = viewController
        }){_ in
            if let completion = completion {
                completion()
            }

        }
    }
}

Here's a solution I've used for this problem. I've no idea how it integrates with Storyboards since I don't use those.

Added this method in a category to UIViewController, then can invoke anywhere previously called presentViewController:animated:completion. Results in a seamless animation of the new controller while still dismissing the previous one.

-(void)presentViewControllerDismissingPrevious:(UIViewController* _Nonnull)controller animated:(BOOL)animated completion:(void (^ __nullable)(void))completion {

    UIViewController* visibleController = self;
    {
        UIViewController* temp;
        while( ( temp = visibleController.presentedViewController ) != nil ) {
            visibleController = temp;
        }
    }

    if( visibleController == self ) {
        // no previous controller to dismiss
        [self presentViewController:controller animated:animated completion:completion];
    } else {
        // create a temporary snapshot of the visible controller's entire window
        // and add to the current view controller's window until animation completed
        UIWindow* visibleWindow = visibleController.view.window;
        UIView* tempView = [visibleWindow snapshotViewAfterScreenUpdates:NO];
        UIView* rootView = self.view.window.subviews[0];
        tempView.frame = [rootView convertRect:visibleWindow.bounds fromView:visibleWindow];
        [rootView addSubview:tempView];

        [self dismissViewControllerAnimated:NO completion:^(){
            [self presentViewController:controller animated:animated completion:^(){
                [tempView removeFromSuperview];
                if( completion ) {
                    completion();
                }
            }];
        }];
    }
}

The both answers were very helpful, here is the Swift 4 version:

func presentHidingBehindScreenSnapshot(viewController: UIViewController,
                                     completion: (() -> (Void))? ) {
if let screenSnapshot = UIApplication.shared.keyWindow?.snapshotView(afterScreenUpdates: false),
  let rootViewController = UIApplication.shared.keyWindow?.rootViewController {
  rootViewController.view.addSubview(screenSnapshot)
  rootViewController.view.bringSubview(toFront: screenSnapshot)

  rootViewController.dismiss(animated: false, completion: {
    rootViewController.present(viewController, animated: false, completion: {
      screenSnapshot.removeFromSuperview()
      if let existingCompletion = completion {
        existingCompletion()
      }
    })
  })
} else {
  #if DEBUG
  fatalError("Can't hide behind snapshot while presenting other view controller")
  #endif
}

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!