如何在SWIFT Xcode iOS中以编程方式延迟启动画面

zkure5ic  于 2022-10-23  发布在  Swift
关注(0)|答案(8)|浏览(357)

我把image放在imageView里,放在LaunchStoreyboard里。我如何才能以编程方式延迟图像时间?
这里是**Launch Screen Guideline from Apple**。

启动画面查看控制器代码如下

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)
    }
}
j1dl9f46

j1dl9f461#

到目前为止,苹果还没有预定义的方法来控制发布会屏幕。以下是一些不是最佳但有效的方法

方法1创建一个单独的ViewController,该ViewController带有启动徽标并创建计时器或执行一些操作(如数据库/加载一些基本的网络调用),这取决于您的应用程序类型。这样,您可以提前准备数据并按住启动屏幕:)
方法#2不是最优的

使用睡眠代码会让应用程序停顿一段时间。

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

r8xiu3jd2#

不建议将整个应用程序设置为等待状态。如果应用程序在完成之前需要做更多的工作,则监视程序可能会因为启动时间太长而终止应用程序。
取而代之的是,你可以这样做来推迟启动屏幕。

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
    }
c3frrgcw

c3frrgcw3#

SWIFT 4.x

让您的应用程序休眠不是一个好做法!
启动你的应用程序应该尽可能快,所以启动屏幕延迟是你不想使用的。
但是,您可以运行loop,在此期间接收器处理来自所有附加输入源的数据,而不是
休眠**:
这将延长发射屏幕的可见度时间。

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
}
zynd9foi

zynd9foi4#

SWIFT 5.x、iOS 13.x.x

修改AppDelegate类中的以下函数在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
}

相反,您需要修改SceneDelegate类中的*Scene**函数,如下所示。LaunchSceen会延迟3秒。

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 }
}

Window变量应该已经存在于SceneDelegate类中,如下所示。

var window: UIWindow?
c9qzyr3d

c9qzyr3d5#

当然,你的应用程序应该处于休眠状态,因为它可能会因为长时间没有响应而被操作系统终止。
如果你的启动屏幕使用的是静态图片,我的做法是使用LaunchScreen.Storyboard中的图片,然后当你的主控制器启动时,在你的主控制器的ViewDidspecar中以与背景相同的图片以方式呈现一个VC(动画设置为FALSE)。
然后,您可以使用您的逻辑来知道何时关闭启动屏幕(VC中的Dismit方法,Animated设置为False)。
实际LaunchScreen到我的VC呈现相同的屏幕的过渡在我看来是难以察觉的。

psViewDidOuar方法可能会被多次调用,在这种情况下,您需要用逻辑来避免第二次向VC呈现启动画面。

1rhkuytd

1rhkuytd6#

创建一个ViewController并使用NSTmer来检测延迟时间。当计时器结束时,按下第一个UIView控制器。
在ViewDidLoad方法中..

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

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

ikfrs5lh7#

SwiftUI

对于SwiftUI,您可以将与接受的答案非常相似的代码放入ContentView.onEmerging中:

struct ContentView: View {

    var body: some View {
        Text("Hello")
        .onAppear {
            Thread.sleep(forTimeInterval: 3.0)
        }
    }
}
bqujaahr

bqujaahr8#

让线程进入睡眠状态不是一个好主意。
我建议您转到SceneDelegate的“will ConnectTo”函数并粘贴这段代码,您就可以开始了。

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 }

相关问题