ios 显示启动画面的时间超过默认秒数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5618163/
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
Displaying splash screen for longer than default seconds
提问by fulvio
Is it possible to display the Default.png for a specified number of seconds? I have a client that wants the splash screen displayed for longer than its current time.
是否可以在指定的秒数内显示 Default.png?我有一个客户希望启动画面的显示时间比当前时间长。
They would like it displayed for 2 - 3 seconds.
他们希望它显示 2 - 3 秒。
回答by rckoenes
No, the default.png
is shown while your app starts up.
不,default.png
会在您的应用程序启动时显示。
You can add a new viewcontroller which will display the default.png
in the application didFinishLoading
.
您可以添加一个新的视图控制器,它将default.png
在应用程序中显示didFinishLoading
。
This way you display the default.png
a bit longer.
这样你显示的default.png
时间会更长一些。
You should only show the default.png
if you are loading data, which could take some time.
As the appstore guidelines state, you should not delay starting of you are any longer than necessary.
default.png
如果您正在加载数据,您应该只显示,这可能需要一些时间。正如应用商店指南所述,您不应延迟启动超过必要的时间。
回答by Chetan Bhalara
You can also use NSThread
:
您还可以使用NSThread
:
[NSThread sleepForTimeInterval:(NSTimeInterval)];
You can put this code in to first line of applicationDidFinishLaunching
method.
您可以将此代码放入applicationDidFinishLaunching
方法的第一行。
For example, display default.png for 5 seconds.
例如,显示 default.png 5 秒。
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
[NSThread sleepForTimeInterval:5.0];
}
回答by Daniel Storm
This is super hacky. Don't do this in production.
这是超级黑客。不要在生产中这样做。
Add this to your application:didFinishLaunchingWithOptions:
:
将此添加到您的application:didFinishLaunchingWithOptions:
:
Swift:
迅速:
// Delay 1 second
RunLoop.current.run(until: Date(timeIntervalSinceNow: 1.0))
Objective C:
目标 C:
// Delay 1 second
[[NSRunLoop currentRunLoop]runUntilDate:[NSDate dateWithTimeIntervalSinceNow: 1.0]];
回答by Kappe
If you are using the LaunchScreen.storyboard you can obtain the same view controller and present it: (remember to set the storyboard id, for example "LaunchScreen")
如果您使用的是 LaunchScreen.storyboard,您可以获得相同的视图控制器并呈现它:(记住设置故事板 id,例如“LaunchScreen”)
func applicationDidBecomeActive(application: UIApplication) {
let storyboard = UIStoryboard(name: "LaunchScreen", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("LaunchScreen")
self.window!.rootViewController!.presentViewController(vc, animated: false, completion: nil)
}
SWIFT 4
快速 4
let storyboard = UIStoryboard(name: "LaunchScreen", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "LaunchScreen")
self.window!.rootViewController!.present(vc, animated: false, completion: nil)
回答by Ehab Saifan
Swift 3
斯威夫特 3
This is doable in a safe way by presenting the splash controller for what ever time you specify then remove it and display your normal rootViewController.
通过在您指定的任何时间呈现飞溅控制器,然后将其删除并显示您的普通 rootViewController,这是可以安全的方式实现的。
- First in LaunchingScreen.storyboard give your controller a StoryBoard identifier let's say "splashController"
- In Main.storyboard give your initial viewController a StoryBoard identifier let's say "initController". -This could be nav or tab bar etc...-
- 首先在 LaunchingScreen.storyboard 中给你的控制器一个 StoryBoard 标识符让我们说“splashController”
- 在 Main.storyboard 中给你的初始 viewController 一个 StoryBoard 标识符让我们说“initController”。- 这可能是导航或标签栏等...-
In AppDelegate you can create these 2 methods:
在 AppDelegate 中,您可以创建以下 2 个方法:
private func extendSplashScreenPresentation(){ // Get a refernce to LaunchScreen.storyboard let launchStoryBoard = UIStoryboard.init(name: "LaunchScreen", bundle: nil) // Get the splash screen controller let splashController = launchStoryBoard.instantiateViewController(withIdentifier: "splashController") // Assign it to rootViewController self.window?.rootViewController = splashController self.window?.makeKeyAndVisible() // Setup a timer to remove it after n seconds Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(dismissSplashController), userInfo: nil, repeats: false) }
private func extendSplashScreenPresentation(){ // Get a refernce to LaunchScreen.storyboard let launchStoryBoard = UIStoryboard.init(name: "LaunchScreen", bundle: nil) // Get the splash screen controller let splashController = launchStoryBoard.instantiateViewController(withIdentifier: "splashController") // Assign it to rootViewController self.window?.rootViewController = splashController self.window?.makeKeyAndVisible() // Setup a timer to remove it after n seconds Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(dismissSplashController), userInfo: nil, repeats: false) }
2.
2.
@objc private func dismissSplashController() {
// Get a refernce to Main.storyboard
let mainStoryBoard = UIStoryboard.init(name: "Main", bundle: nil)
// Get initial viewController
let initController = mainStoryBoard.instantiateViewController(withIdentifier: "initController")
// Assign it to rootViewController
self.window?.rootViewController = initController
self.window?.makeKeyAndVisible()
}
Now you call
现在你打电话
self.extendSplashScreenPresentation()
in didFinishLaunchingWithOptions.
在 didFinishLaunchingWithOptions 中。
You are set to go...
你准备去...
回答by MB_iOSDeveloper
This worked for me in Xcode 6.3.2, Swift 1.2 :
这在 Xcode 6.3.2, Swift 1.2 中对我有用:
import UIKit
class ViewController: UIViewController
{
var splashScreen:UIImageView!
override func viewDidLoad()
{
super.viewDidLoad()
self.splashScreen = UIImageView(frame: self.view.frame)
self.splashScreen.image = UIImage(named: "Default.png")
self.view.addSubview(self.splashScreen)
var removeSplashScreen = NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: "removeSP", userInfo: nil, repeats: false)
}
func removeSP()
{
println(" REMOVE SP")
self.splashScreen.removeFromSuperview()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
}
ViewControlleris the first app VC that is being loaded.
ViewController是正在加载的第一个应用程序 VC。
回答by visakh7
This tutorialdisplays splash screen for 2 seconds. You can easily change it to suit your needs.
本教程显示启动画面 2 秒钟。您可以轻松更改它以满足您的需求。
- (void)showSplash {
UIViewController *modalViewController = [[UIViewController alloc] init];
modalViewController.view = modelView;
[self presentModalViewController:modalViewController animated:NO];
[self performSelector:@selector(hideSplash) withObject:nil afterDelay:yourDelay];
}
回答by Mr. Roddy
In Xcode 6.1, Swift 1.0 to delay the launch screen:
在 Xcode 6.1、Swift 1.0 中延迟启动屏幕:
Add this to the didFinishLaunchingWithOptions
将此添加到 didFinishLaunchingWithOptions
NSThread.sleepForTimeInterval(3)
the time in the () is variable.
() 中的时间是可变的。
In new swift
在新的快速
Thread.sleep(forTimeInterval:3)
回答by Himanshu Mahajan
Use following line in didFinishLaunchingWithOptions:
delegate method:
在didFinishLaunchingWithOptions:
委托方法中使用以下行:
[NSThread sleepForTimeInterval:5.0];
It will stop splash screen for 5.0 seconds.
它将停止闪屏 5.0 秒。
回答by A.G
Swift 2.0:
斯威夫特 2.0:
1)
1)
// AppDelegate.swift
import UIKit
import Foundation
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var splashTimer:NSTimer?
var splashImageView:UIImageView?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window = UIApplication.sharedApplication().delegate!.window!
let splashImage: UIImage = UIImage(named: "ic_120x120.png")!
splashImageView = UIImageView(image: splashImage)
splashImageView!.frame = CGRectMake(0, 0, (window?.frame.width)!, (window?.frame.height)!)
window!.addSubview(splashImageView!)
window!.makeKeyAndVisible()
//Adding splash Image as UIWindow's subview.
window!.bringSubviewToFront(window!.subviews[0])
// Here specify the timer.
splashTimer = NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: "splashTimerForLoadingScreen", userInfo: nil, repeats: true)
return true
}
func splashTimerForLoadingScreen() {
splashImageView!.removeFromSuperview()
splashTimer!.invalidate()
}
2)
2)
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
NSThread.sleepForTimeInterval(9)
OR
sleep(9)
return true
}
3) Using root view controller concept:
3) 使用根视图控制器概念:
// AppDelegate.swift
import UIKit
import Foundation
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var splashTimer:NSTimer?
var storyboard:UIStoryboard?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.makeKeyAndVisible()
storyboard = UIStoryboard(name: "Main", bundle: nil)
//Here set the splashScreen VC
let rootController = storyboard!.instantiateViewControllerWithIdentifier("secondVCID")
if let window = self.window {
window.rootViewController = rootController
}
//Set Timer
splashTimer = NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: "splashTimerCrossedTimeLimit", userInfo: nil, repeats: true)
return true
}
func splashTimerCrossedTimeLimit(){
//Here change the root controller
let rootController = storyboard!.instantiateViewControllerWithIdentifier("firstVCID")
if let window = self.window {
window.rootViewController = rootController
}
splashTimer?.invalidate()
}