首页 > 解决方案 > Swift - 无论哪个 ViewController 在应用程序启动时显示revealingSplashView

问题描述

在我的应用程序中,我使用了revealingSplashView

用户既可以登录,也可以ViewController更改开始:

    class MainNavigationControllerViewController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        if isLoggedIn() {
            let homeController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomeVC")
            viewControllers = [homeController]
        }
    }

    fileprivate func isLoggedIn() -> Bool {
        return UserDefaults.standard.isLoggedIn()
    }
}

或者用户未登录,因此FirstLaunchViewController正在显示用户可以登录和访问的位置MainViewController

目前我是这样介绍revealingSplashViewFirstLaunchViewController

let revealingSplashView = RevealingSplashView(iconImage: UIImage(named: "zauberstab")!, iconInitialSize: CGSize(width: 120, height: 120), backgroundColor: .white)

revealingSplashView.startAnimation()

问题是,如果用户已登录,我也希望拥有该动画,因此MainViewController启动 VC 也是如此。我知道我可以将代码从FirstLaunchViewControllerto复制,MainViewController但是当用户登录并进入MainViewController正在显示的动画时,这会导致问题,即使它应该只在应用程序启动后显示。

标签: iosswiftanimationuiviewuiviewcontroller

解决方案


像@chirag90 建议的解决方案是将代码移入AppDelegate如下didFinishLaunchingWithOptions

var window: UIWindow?

let revealingSplashView = RevealingSplashView(iconImage: UIImage(named: "zauberstab")!, iconInitialSize: CGSize(width: 120, height: 120), backgroundColor: .white)

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    revealingSplashView.startAnimation()
    window?.rootViewController?.view.addSubview(revealingSplashView)

    return true
}

出现的另一个问题是如何添加reavelingSplashView一个Subview@matt 在这个问题中回答的问题:

在 AppDelegate 中添加子视图


推荐阅读