[英]Dismiss and present modal view controller with one animation
问题是我不知道如何解雇和呈现只有一个过渡动画的视图控制器。
我的故事板结构是:
我们可以说A控制器是在NavigationController之后, B是Startup引用, C是TabBar ViewController。 B和C都以模态方式呈现交叉溶解过渡。
当用户登录到应用程序(来自B )时, C控制器以模态方式呈现,具有翻转水平过渡。 当用户注销(来自C )时, B以相同的方式呈现。 在A控制器上,我根据用户是否记录执行直接segue到B或C.
我的问题是如果我不从B或C中解除以前的视图控制器,那么该控制器就会泄露。 相反,如果我解除它,则在呈现目标控制器( B或C )之前显示A.
是否可以仅显示“ 水平翻转”和“查看”视图?
我对这个问题的解决方案是替换当前的rootViewController,支持不同的转换:
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()
}
}
}
}
这是我用于此问题的解决方案。 我不知道它是如何与Storyboard集成的,因为我不使用它们。
在UIViewController的类别中添加了此方法,然后可以调用以前称为presentViewController:animated:completion
任何地方presentViewController:animated:completion
。 导致新控制器的无缝动画,同时仍然消除前一个控制器。
-(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();
}
}];
}];
}
}
这两个答案都非常有用,这里是Swift 4版本:
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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.