How can I delay splash launch screen programmatically in Swift Xcode iOS

泪湿孤枕 提交于 2020-05-09 18:33:06

问题


I have put an image in imageView in LaunchStoreyboard. How can I delay the time of image programmatically?

Here is the Launch Screen Guideline from Apple.

Here is code for Launch Screen View controller:

import UIKit
class LaunshViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.delay(0.4)
    }

    func delay(_ delay:Double, closure:@escaping ()->()) {
        let when = DispatchTime.now() + delay
        DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
    }
}

回答1:


Put one line of code in AppDelegate class -

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        Thread.sleep(forTimeInterval: 3.0)
        // Override point for customization after application launch.
        return true
    }



回答2:


Swift 4.x

It is Not a good practice to put your application to sleep!

Booting your App should be as fast as possible, so the Launch screen delay is something you do not want to use.

But, instead of sleeping you can run a loop during which the receiver processes data from all attached input sources:

This will prolong the launch-screen's visibility time.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    RunLoop.current.run(until: NSDate(timeIntervalSinceNow:1) as Date)

    return true
}



回答3:


Would not recommending setting the entire application in a waiting state. If the application needs to do more work before finishing the watchdog could kill the application for taking too long time to start up.

Instead you could do something like this to delay the launch screen.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = UIStoryboard(name: "LaunchScreen", bundle: nil).instantiateInitialViewController()
        window?.makeKeyAndVisible()

        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
            self.window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
        }
        return true
    }



回答4:


Create a ViewController and use NSTimer to detect the delay time. and when the timer ends push the first UIViewcontroller.

In ViewDidLoad method..

[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(fireMethod) userInfo:nil repeats:NO];


-(void)fireMethod
{
// push view controller here..
}



回答5:


Definitely your app should not be put to sleep as it may be killed by the OS for being unresponsive for so long.

If you're using a static image for your launch screen, what works for me is to use the image in the LaunchScreen.storyboard, and then when your main controller launches, modally present a VC with the same image as the background in the ViewDidAppear of your main controller (with animated set to false).

You can then use your logic to know when to dismiss the launch screen (dismiss method in the VC with animated set to false).

The transition from the actual LaunchScreen to my VC presenting the same screen looks to me imperceptible.

PS: the ViewDidAppear method might be called more than once, in which case you need to use logic to not present the VC with the launch screen a second time.




回答6:


Swift 5.x, iOS 13.x.x

Modifying the following function in the AppDelegate class does not work in Swift 5.x/iOS 13.x.x.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    return true
}

Instead, you will have to modify the scene function in SceneDelegate class as following. It will delay the LaunchSceen for 3 seconds.

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

    window?.rootViewController = UIStoryboard(name: "LaunchScreen", bundle: nil).instantiateInitialViewController()
    window?.makeKeyAndVisible()

    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
        self.window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
    }

    guard let _ = (scene as? UIWindowScene) else { return }
}

The window variable should already be there in SceneDelegate class like the following.

var window: UIWindow?


来源:https://stackoverflow.com/questions/43276199/how-can-i-delay-splash-launch-screen-programmatically-in-swift-xcode-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!