【发布时间】:2011-08-02 20:30:39
【问题描述】:
是否可以在指定的秒数内显示 Default.png?我有一个客户希望启动画面显示的时间比当前时间长。
他们希望它显示 2 到 3 秒。
【问题讨论】:
标签: ios objective-c splash-screen
是否可以在指定的秒数内显示 Default.png?我有一个客户希望启动画面显示的时间比当前时间长。
他们希望它显示 2 到 3 秒。
【问题讨论】:
标签: ios objective-c splash-screen
不,default.png 会在您的应用启动时显示。
您可以添加一个新的视图控制器,它将在应用程序didFinishLoading 中显示default.png。
这样您可以将default.png 显示得更长一点。
只有在加载数据时才应显示default.png,这可能需要一些时间。
正如应用商店指南所述,您不应在必要时延迟启动。
【讨论】:
实现此目的的最简单方法是在您的第一个 ViewController 的 UIView 顶部创建一个带有“Default.png”的 UIImageView。
并添加一个计时器以在您预期的几秒钟后删除UIImageView。
【讨论】:
将您的 default.png 作为主视图顶部的子视图放在 UIImageView 全屏中,从而覆盖您的其他 UI。设置一个计时器以在 x 秒后将其移除(可能带有效果)现在显示您的应用程序。
【讨论】:
你也可以使用NSThread:
[NSThread sleepForTimeInterval:(NSTimeInterval)];
你可以把这段代码放到applicationDidFinishLaunching方法的第一行。
例如,显示 default.png 5 秒。
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
[NSThread sleepForTimeInterval:5.0];
}
【讨论】:
此tutorial 显示启动画面 2 秒。您可以轻松更改它以满足您的需求。
- (void)showSplash {
UIViewController *modalViewController = [[UIViewController alloc] init];
modalViewController.view = modelView;
[self presentModalViewController:modalViewController animated:NO];
[self performSelector:@selector(hideSplash) withObject:nil afterDelay:yourDelay];
}
【讨论】:
您可以使用以下代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSMutableString *path = [[NSMutableString alloc]init];
[path setString:[[NSBundle mainBundle] resourcePath]];
[path setString:[path stringByAppendingPathComponent:@"Default.png"]];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:path];
[path release];
UIImageView *imageView=[[UIImageView alloc]initWithImage:image];
imageView.frame=CGRectMake(0, 0, 320, 480);
imageView.tag = 2;
[window addSubview:imageView];
[window makeKeyAndVisible];
// Here specify the time limit.
timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(timerForLoadingScreen) userInfo:nil repeats:YES];
}
-(void)timerForLoadingScreen
{
[timer invalidate];
if ([window viewWithTag:2]!=nil)
{
[[window viewWithTag:2]removeFromSuperview];
}
// Your any other initialization code that you wish to have in didFinishLaunchingWithOptions
}
【讨论】:
您可以在 AppDelegate didFinishLaunchingWithOptions 方法中简单地指定休眠秒数。
或者使用另一个 ImageView 来自定义启动画面。
在我的以下链接中查看后者的详细信息:
【讨论】:
写sleep(5.0)
在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions会显示5秒闪屏
【讨论】:
在didFinishLaunchingWithOptions: 委托方法中使用以下行:
[NSThread sleepForTimeInterval:5.0];
它将停止启动画面 5.0 秒。
【讨论】:
这行得通...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Load Splash View Controller first
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"Splash"];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
// Load other stuff that requires time
// Now load the main View Controller that you want
}
【讨论】:
在 Xcode 6.1、Swift 1.0 中延迟启动屏幕:
在 AppDelegateod 中的 e didFinishLaunchingWithOptionsmeth 中添加以下语句
NSThread.sleepForTimeInterval(3)
这里,时间可以根据你的要求过去。
SWIFT 5
Thread.sleep(forTimeInterval: 3)
【讨论】:
这在 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()
}
}
ViewController 是第一个正在加载的应用程序 VC。
【讨论】:
如果您使用的是 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
let storyboard = UIStoryboard(name: "LaunchScreen", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "LaunchScreen")
self.window!.rootViewController!.present(vc, animated: false, completion: nil)
【讨论】:
只要继续项目名称。然后右键单击/属性/应用程序选项卡。
在 Slash 表单组合框附近找到“查看应用程序事件”。
将此代码复制到myApplication 类中:
Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
System.Threading.Thread.Sleep(3000) ' or other time
End Sub
【讨论】:
Swift 2.0:
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)
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
NSThread.sleepForTimeInterval(9)
OR
sleep(9)
return true
}
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()
}
【讨论】:
这是超级hacky。不要在生产环境中这样做。
将此添加到您的application:didFinishLaunchingWithOptions::
斯威夫特:
// Delay 1 second
RunLoop.current.run(until: Date(timeIntervalSinceNow: 1.0))
目标 C:
// Delay 1 second
[[NSRunLoop currentRunLoop]runUntilDate:[NSDate dateWithTimeIntervalSinceNow: 1.0]];
【讨论】:
1.在“didFinishLaunchingWithOptions”中添加另一个视图控制器
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UINavigationController *homeNav = [storyboard instantiateViewControllerWithIdentifier:@"NavigationControllerView"];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"SplashViewController"];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = homeNav;
[self.window makeKeyAndVisible];
[(UINavigationController *)self.window.rootViewController pushViewController:viewController animated:NO];
}
2.在视图中确实加载了SplashView Controller
[self performSelector:@selector(removeSplashScreenAddViewController) withObject:nil afterDelay:2.0];
3.在 removeSplashScreenAddViewController 方法中,您可以添加主视图控制器,例如。-
- (void) removeSplashScreenAddViewController {` UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UINavigationController *homeNav = [storyboard instantiateViewControllerWithIdentifier:@"HomeNav"];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:viewControllerName];
UIWindow *window = [StaticHelper mainWindow];
window.rootViewController = homeNav;
[window makeKeyAndVisible];
[(UINavigationController *)window.rootViewController pushViewController:viewController animated:NO];`}
【讨论】:
您可以创建自己的视图并在应用程序启动时显示它并使用计时器隐藏它。请避免延迟应用启动作为它的坏主意
【讨论】:
斯威夫特 3
这是通过在您指定的任何时间呈现启动控制器然后将其移除并显示您的正常 rootViewController 以安全的方式实现的。
在 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)
}
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()
}
现在你打电话
self.extendSplashScreenPresentation()
在 didFinishLaunchingWithOptions 中。
你准备好了……
【讨论】:
这里最简单的解决方案是在AppDelegate 类中将sleep() 添加到didFinishLaunchingWithOptions 方法。
斯威夫特 4:
sleep(1)
如果你想做一些更高级的事情,你也可以用同样的方法扩展当前的 RunLoop:
斯威夫特 4:
RunLoop.current.run(until: Date(timeIntervalSinceNow: 1))
【讨论】:
在 swift 4.0 中
默认启动时间后延迟 1 秒...
RunLoop.current.run(until: Date(timeIntervalSinceNow : 1.0))
【讨论】:
在 Swift 4.2 中
默认启动时间后延迟 1 秒...
Thread.sleep(forTimeInterval: 1)
【讨论】: