简体   繁体   中英

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.

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.

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. :)

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.

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.

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 . But UIViewPropertyAnimator is only compliant with iOS 10+.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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