如何在swift 3中将视图控制器设置为闪屏?

Jes*_*son 2 uiviewcontroller ios swift3

我试图使用swift 3在storyboard中创建一个视图控制器作为我的应用程序中的启动画面.我正在尝试使用"启动图像"加载启动时实现与启动画面相同的效果.由于我在启动画面中有动画,我希望在Swift 3中使用视图控制器作为启动画面.如何实现这一目标?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    self.window = UIWindow(frame: UIScreen.main.bounds)

    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    let initialViewController = storyboard.instantiateViewController(withIdentifier: "splashScreen")

    self.window?.rootViewController = initialViewController
    self.window?.makeKeyAndVisible()
    return true
}
Run Code Online (Sandbox Code Playgroud)

Him*_*shu 11

尝试将视图控制器设置为具有闪屏的rootViewController,并在显示视图控制器2-3秒后.推/显您的初始视图控制器.

你可以这样设置启动ViewController: -

self.window?.rootViewController = splashVC
Run Code Online (Sandbox Code Playgroud)

在splashVC的ViewDidLoad()中使用定时器或延迟并在2-3秒后使用.显示应用程序的主View Controller.

//MARK:- Life Cycle Methods
    override func viewDidLoad() {
        super.viewDidLoad()
        Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.splashTimeOut(sender:)), userInfo: nil, repeats: false)
        // Do any additional setup after loading the view.
    }

func splashTimeOut(sender : Timer){
    AppDelegate.sharedInstance().window?.rootViewController = yourCustomViewController
    }
Run Code Online (Sandbox Code Playgroud)

为方便起见,在Appdelegate中添加此方法: -

class func sharedInstance() -> AppDelegate{
        return UIApplication.shared.delegate as! AppDelegate
    }
Run Code Online (Sandbox Code Playgroud)