gpt4 book ai didi

swift - 用动画交换 rootViewController?

转载 作者:IT王子 更新时间:2023-10-29 05:00:04 31 4
gpt4 key购买 nike

我正在尝试切换到另一个带有标签栏的 Root View Controller ;通过应用程序委托(delegate),我想添加过渡动画。默认情况下,它只会显示没有任何动画的 View 。

let tabBar = self.instantiateViewController(storyBoard: "Main", viewControllerID: "MainTabbar")
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window = UIWindow(frame: UIScreen.main.bounds)
appDelegate.window?.rootViewController = tabBar
appDelegate.window?.makeKeyAndVisible()

这就是我切换到另一个 Root View Controller 的方式。

最佳答案

您可以使用 UIView.transition(with: view) 来替换 UIWindowrootViewController:

guard let window = UIApplication.shared.keyWindow else {
return
}

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MainTabbar")

// Set the new rootViewController of the window.
// Calling "UIView.transition" below will animate the swap.
window.rootViewController = vc

// A mask of options indicating how you want to perform the animations.
let options: UIView.AnimationOptions = .transitionCrossDissolve

// The duration of the transition animation, measured in seconds.
let duration: TimeInterval = 0.3

// Creates a transition animation.
// Though `animations` is optional, the documentation tells us that it must not be nil. ¯\_(ツ)_/¯
UIView.transition(with: window, duration: duration, options: options, animations: {}, completion:
{ completed in
// maybe do something on completion here
})

关于swift - 用动画交换 rootViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41144523/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com