使用一个动画关闭并呈现模态视图控制器 [英] Dismiss and present modal view controller with one animation

查看:156
本文介绍了使用一个动画关闭并呈现模态视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是我不知道如何解雇并呈现只有一个过渡动画的视图控制器。

The problem is that I don't know how to dismiss and present a view controller with only one transition animation.

我的故事板结构是:


我们可以说 A 控制器就在NavigationController之后, B 是启动参考, C 是TabBar ViewController。 B C 都以模态方式呈现 Cross Dissolve 转换。

We can say that A controller is what follows the NavigationController, B is the Startup reference and C is the TabBar ViewController. Both B and C are presented modally with a Cross Dissolve transition.

当用户登录应用程序时(来自 B ), C 控制器以模态方式显示翻转水平过渡。当用户注销(来自 C )时, B 以相同的方式显示。
A 控制器上,我执行直接转换为 B C ,具体取决于用户是否已登录。

When a user logins into the app (from B), C controller is presented modally with a Flip horizontal transition. And when a user logouts (from C), B is presented in the same way. On A controller I perform a direct segue to B or C depending on if user is logged or not.

我的问题是,如果我不从 B C 中解除以前的视图控制器,则该控制器会泄露。相反,如果我将其解雇,则会在目标控制器( B C )出现之前显示 A

My problem is that if I don't dismiss previous view controller from B or C, that controller is leaked. On the contrary if I dismiss it, A is showed before the target controller (B or C) is presented.

是否可以仅显示水平翻转过渡并跳过 A 视图?

Is it possible to show only the Flip Horizontal transition and step over A view?

推荐答案

我对此问题的解决方案是替换当前的rootViewController,支持不同的转换:

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

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

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

        }
    }
}

这篇关于使用一个动画关闭并呈现模态视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆