如何在 Swift Xcode iOS 中以编程方式延迟启动屏幕
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43276199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I delay splash launch screen programmatically in Swift Xcode iOS
提问by Xcodian Solangi
I have put an image
in imageView
in LaunchStoreyboard
. How can I delay the time of image programmatically?
我已经image
输入imageView
了LaunchStoreyboard
。如何以编程方式延迟图像的时间?
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)
}
}
回答by Hyman
Put one line of code in AppDelegate class -
在 AppDelegate 类中放入一行代码 -
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Thread.sleep(forTimeInterval: 3.0)
// Override point for customization after application launch.
return true
}
回答by yoowim
Swift 4.x
斯威夫特 4.x
It is Nota 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 sleepingyou can run a loop
during which the receiver processes data from all attached input sources:
但是,您可以运行 a代替睡眠,loop
在此期间接收器处理来自所有附加输入源的数据:
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
}
回答by oscar
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
}
回答by Saranjith
Create a ViewController and use NSTimer to detect the delay time. and when the timer ends push the first UIViewcontroller.
创建一个 ViewController 并使用 NSTimer 检测延迟时间。当计时器结束时推送第一个 UIView 控制器。
In ViewDidLoad method..
在 ViewDidLoad 方法中..
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(fireMethod) userInfo:nil repeats:NO];
-(void)fireMethod
{
// push view controller here..
}
回答by raulmf9325
Definitely your app should notbe 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 ViewDidAppearof your main controller (with animated set to false).
如果您在启动屏幕上使用静态图像,对我有用的是使用LaunchScreen.storyboard 中的图像,然后当您的主控制器启动时,以模态呈现与ViewDidAppear 中的背景图像相同的 VC主控制器的(动画设置为 false)。
You can then use your logic to know when to dismiss the launch screen (dismissmethod in the VC with animated set to false).
然后,您可以使用您的逻辑来了解何时关闭启动屏幕(VC 中的dismiss方法将动画设置为false)。
The transition from the actualLaunchScreen to my VC presenting the same screen looks to me imperceptible.
从实际LaunchScreen 到我的 VC 呈现相同屏幕的过渡在我看来难以察觉。
PS: the ViewDidAppearmethod 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.
PS:可能会多次调用ViewDidAppear方法,在这种情况下,您需要使用逻辑来避免第二次向 VC 显示启动屏幕。
回答by alvin
Swift 5.x, iOS 13.x.x
斯威夫特 5.x,iOS 13.xx
Modifying the following function in the AppDelegateclass does not work in Swift 5.x/iOS 13.x.x.
在AppDelegate类中修改以下函数在Swift 5.x/iOS 13.xx 中不起作用。
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 scenefunction in SceneDelegateclass as following. It will delay the LaunchSceen for 3 seconds.
相反,您必须修改SceneDelegate类中的场景函数,如下所示。它会延迟 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 }
}
The windowvariable should already be there in SceneDelegateclass like the following.
该窗口变量应该已经有在SceneDelegate像下面的类。
var window: UIWindow?