简体   繁体   English

如何在UINavigationController屏幕过渡时翻转整个屏幕?

[英]How to flip the whole screen at UINavigationController screen transition?

I have a logout button that can be accessed at any screen, that when tapped, I want to pop out all of the view controllers currently on the stack of navigation controller, and change into just a login screen as the root view controller of the navigation controller. 我有一个注销按钮,可以在任何屏幕上访问该按钮,点击该按钮时,我想弹出导航控制器堆栈中当前存在的所有视图控制器,然后切换为登录屏幕作为导航的根视图控制器控制器。 But I want that the transition is like flipping the whole screen. 但我希望这种过渡就像翻转整个屏幕一样。

I see some code that says to use: 我看到一些使用的代码:

UIView.transitionFromView(frontView, toView: backView, duration: 1, options: UIViewAnimationOptions.TransitionFlipFromRight | UIViewAnimationOptions.ShowHideTransitionViews, completion: nil)

But this is to transition just a UIView, not the whole screen. 但这只是过渡一个UIView,而不是整个屏幕。

More over, this probably can be used to change the whole screen if I supply self.view as the frontView, but that means I only change the frontView of the current top view controller, and not change anything of the view controllers stack in navigation controller. 而且,如果我将self.view作为frontView提供,则这可能可以用于更改整个屏幕,但这意味着我仅更改了当前顶视图控制器的frontView,而不更改导航控制器中的任何视图控制器堆栈。

How can I flip the whole screen, and at the same time change the stack of the navigation controller? 如何翻转整个屏幕,同时更改导航控制器的堆栈?

I imagine there is probably some code like: 我想可能有一些类似的代码:

func logout() {
    let loginVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "login")
    UIView.animate (duration: 0.3, options: UIViewAnimationOptions.TransitionFlipFromRight) {
        self.navigationController?.viewControllers = [loginVc];
    }
}

I have tried this but the change happens in an instant, instead of animating. 我已经尝试过了,但是更改是即时发生的,而不是动画。

Thanks. 谢谢。

EDIT: 编辑:

To clarify what I want, is that I want from: 为了澄清我想要的是我想要的:

UINavigationController with stack: [Vc1, Vc2, Vc3, Vc4, Vc5, ...]

To: 至:

UINavigationController with stack: [LoginVc]

using an entire screen flip animation. 使用整个屏幕翻转动画。

Whether this is done by popping to root or replacing the stack or replacing the UINavigationController entirely, whether it's using UIView.animate or UIView.transitionFromView or others, or any combination of the above, that's up to the good people who helps with the solutions. 无论是通过弹出根目录还是替换堆栈还是完全替换UINavigationController来完成此操作,无论是使用UIView.animate还是UIView.transitionFromView或其他方法,还是上述方法的任意组合,都取决于为解决方案提供帮助的优秀人员。 :) :)

There are a number of ways you can accomplish this and your best solution is some combination of how it's currently set up, what your UI allows, and personal preference. 您可以通过多种方式来完成此任务,最好的解决方案是将其当前设置方式,UI允许的功能以及个人喜好结合在一起。

For example, when the user hits the logout button, you could present a view controller modally with the login form using a flip animation and pop the nav to its root in the background (which the user would not see). 例如,当用户单击注销按钮时,您可以使用翻转动画以模版形式显示带有登录表单的视图控制器,并将导航栏弹出到其在后台的根目录(用户看不到)。 And so when the user logs in, dismissing the view controller would take the user to the nav's root. 因此,当用户登录时,解雇视图控制器会将用户带到导航的根目录。

Another option may be giving the root view controller two different states. 另一个选择可能是给根视图控制器两个不同的状态。 When the user is logged in, content is normally shown. 用户登录后,通常会显示内容。 And when the user is logged out, the login form is shown. 当用户注销后,将显示登录表单。 You can accomplish this very basically using isHidden property if the view controller is relatively simple. 如果视图控制器相对简单,则可以使用isHidden属性非常基本地完成此操作。

Whatever route you take, the easiest way to make a flip animation is this: 无论采取哪种方式,制作翻转动画的最简单方法是:

guard let nav = navigationController else {
    return
}

UIView.transition(with: nav.view, duration: 0.75, options: .transitionFlipFromLeft, animations: {
    nav.popToRootViewController(animated: false)
}, completion: nil)

Your flip options: 您的翻转选项:

.transitionFlipFromLeft
.transitionFlipFromRight
.transitionFlipFromTop
.transitionFlipFromBottom

The use of this method, as are all UIView animation methods, is now discouraged by Apple since the introduction of UIViewPropertyAnimator . 自从引入UIViewPropertyAnimator以来,Apple现在不鼓励使用此方法以及所有UIView动画方法。 But UIViewPropertyAnimator is only compliant with iOS 10+. 但是, UIViewPropertyAnimator仅与iOS 10+兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM