iOS:如何延迟启动屏幕?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35028449/
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
iOS: how to delay the launch screen?
提问by ishahak
When launch an app, the LaunchScreen.xibis removed as soon as all the assets are initialized.
启动应用程序时,一旦所有资产都初始化,LaunchScreen.xib 就会被删除。
I want to make the launch screen stay for at least 1 sec.
我想让启动屏幕停留至少 1 秒。
Is there a way to achieve this?
有没有办法实现这一目标?
Thank you!
谢谢!
回答by EmilioPelaez
You can create a view controller that uses the LaunchScreen storyboard, present it (not animated) on applicationDidFinishLaunching
or applicationWillFinishLaunching
, and dismiss it whenever you want.
您可以创建一个使用 LaunchScreen 故事板的视图控制器,在applicationDidFinishLaunching
或上呈现它(非动画)applicationWillFinishLaunching
,并在需要时关闭它。
Keep in mind this is discouraged by Apple because it gives the impression that your app takes a lot longer to launch, which is bad user experience and might cause some of your users to delete your app.
请记住,Apple 不鼓励这样做,因为它给人的印象是您的应用程序需要更长的时间才能启动,这是糟糕的用户体验,并可能导致您的某些用户删除您的应用程序。
回答by Xcodian Solangi
Swift 4 Update
斯威夫特 4 更新
Just write one line of code
Thread.sleep(forTimeInterval: 3.0)
in the method of didfinishLauching....
in appdelegate
class.
在类Thread.sleep(forTimeInterval: 3.0)
的方法中写一行代码即可
。didfinishLauching....
appdelegate
Example
例子
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Thread.sleep(forTimeInterval: 3.0)
// Override point for customization after application launch.
return true
}
回答by Xcoder
Neversleep on the main thread. Doing this could actually cause iOS to kill your app for taking too long to start up
永远不要在主线程上睡觉。这样做实际上可能会导致 iOS 因启动时间过长而终止您的应用程序
回答by GeneCode
Thought I chip in my thoughts on this, I wanted to write in comments but it won't allow many lines. I believe many apps developer want to do this (delay the launch screen) is because they want to build a brand presence of the apps/games for their company.
我想我对此有自己的想法,我想写在评论中,但它不允许有很多行。我相信许多应用程序开发人员想要这样做(延迟启动屏幕)是因为他们想为他们的公司建立应用程序/游戏的品牌形象。
Having said that, launch screen is NOT designed for that, as Rick Maddy explained in the comment section in one of the other answers. Launch screen's purpose is to make users feel the app is instantly running by showing the empty UI while the actual data is loading at the back (willAppear and so on).
话虽如此,启动屏幕并不是为此而设计的,正如 Rick Maddy 在其他答案之一的评论部分中所解释的那样。启动屏幕的目的是通过在实际数据加载在后面(willAppear 等)时显示空 UI,让用户感觉应用程序立即运行。
So to achieve what many developers want, while being in-line with Apple's HIG, what you can do is:
因此,要实现许多开发人员想要的,同时与 Apple 的 HIG 保持一致,您可以做的是:
- Display UI template in the launchscreen as intended by Apple HIG.
- Upon main screen load, load up another VC that shows "intro" of your brand. Make sure this runs only ONCE (a simple flag in NSUserDefaults should do the trick).
- Users should be allowed to skip this if it is a long "intro".
- 按照 Apple HIG 的意图在启动屏幕中显示 UI 模板。
- 在主屏幕加载时,加载另一个显示您品牌“介绍”的 VC。确保这只运行一次(NSUserDefaults 中的一个简单标志应该可以解决问题)。
- 如果它是一个很长的“介绍”,应该允许用户跳过它。
The same "intro" VC should be available to user by tapping on a "View Intro" button somewhere (maybe in about page).
通过点击某处的“查看介绍”按钮(可能在关于页面中),用户应该可以使用相同的“介绍”VC。
回答by iBhavin
If you want to go with simple, you can use NSThread
:
如果你想简单,你可以使用NSThread
:
[NSThread sleepForTimeInterval:(NSTimeInterval)];
You can put this code into the first line of applicationDidFinishLaunching
method.
您可以将此代码放入applicationDidFinishLaunching
方法的第一行。
For example, display default.png for 1.0 seconds.
例如,显示 default.png 1.0 秒。
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
[NSThread sleepForTimeInterval:1.0];
}
It will stop splash screen for 1.0 seconds.
它将停止闪屏 1.0 秒。