显示启动画面的时间超过默认秒数

时间:2011-04-11 07:32:41

标签: ios objective-c splash-screen

是否可以在指定的秒数内显示Default.png?我有一个客户端希望显示启动画面的时间超过当前时间。

他们希望它显示2-3秒。

22 个答案:

答案 0 :(得分:56)

不,您的应用启动时会显示default.png

您可以添加一个新的viewcontroller,它会在应用default.png中显示didFinishLoading

这样可以显示default.png更长的时间。

如果要加载数据,则只应显示default.png,这可能需要一些时间。 正如appstore指南所述,您不应该延迟启动时间超过必要时间。

答案 1 :(得分:34)

您还可以使用NSThread

[NSThread sleepForTimeInterval:(NSTimeInterval)];

您可以将此代码放入applicationDidFinishLaunching方法的第一行。

例如,显示default.png 5秒钟。

- (void) applicationDidFinishLaunching:(UIApplication*)application
{
   [NSThread sleepForTimeInterval:5.0];
}

答案 2 :(得分:15)

将此添加到您的application:didFinishLaunchingWithOptions:

夫特:

// Delay 1 second
RunLoop.current.run(until: Date(timeIntervalSinceNow: 1.0))

目标C:

// Delay 1 second
[[NSRunLoop currentRunLoop]runUntilDate:[NSDate dateWithTimeIntervalSinceNow: 1.0]];

答案 3 :(得分:7)

如果您使用的是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)

答案 4 :(得分:5)

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

答案 5 :(得分:5)

didFinishLaunchingWithOptions:委托方法中使用以下行:

[NSThread sleepForTimeInterval:5.0];

它将停止启动画面5.0秒。

答案 6 :(得分:5)

在Xcode 6.1中,Swift 1.0延迟启动屏幕:

将其添加到didFinishLaunchingWithOptions

NSThread.sleepForTimeInterval(3)

()中的时间是可变的。

在新的快速

Thread.sleep(forTimeInterval:3)

答案 7 :(得分:5)

这在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。

答案 8 :(得分:4)

在swift 4.0中
在默认启动时间后延迟1秒......

var optionsBuilderType = typeof(DbContextOptionsBuilder<>).MakeGenericType(dbContextType);
var optionsBuilder = (DbContextOptionsBuilder)Activator.CreateInstance(optionsBuilderType);

string connectionString = ...;
optionsBuilder.UseSqlServer(connectionString);

var dbContext = (DbContext)Activator.CreateInstance(dbContextType, optionsBuilder.Options);

答案 9 :(得分:4)

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

答案 10 :(得分:3)

您可以使用以下代码:

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

答案 11 :(得分:3)

Swift 3

通过在指定的时间内显示启动控制器然后将其删除并显示正常的rootViewController,可以安全地实现这一点。

  1. 首先在LaunchingScreen.storyboard中为控制器提供一个StoryBoard标识符,让我们说“splashController”
  2. 在Main.storyboard中为初始viewController提供一个StoryBoard标识符,让我们说“initController”。 - 这可能是导航或标签栏等......-
  3. 在AppDelegate中,您可以创建以下两种方法:

    1. 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()
      }
      

      现在你打电话

       self.extendSplashScreenPresentation()
      

      在didFinishLaunchingWithOptions中。

      你准备好了......

答案 12 :(得分:2)

撰写sleep(5.0)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中显示5秒的启动画面

答案 13 :(得分:2)

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控制器

  [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];`}

答案 14 :(得分:1)

实现此目标的最简单方法是在第一个ViewController UIImageView的顶部创建一个带有“Default.png”的UIView

并添加一个计时器,在您预期的几秒后删除UIImageView

答案 15 :(得分:1)

此处最简单的解决方案是在sleep()课程中添加didFinishLaunchingWithOptionsAppDelegate方法。

Swift 4:

sleep(1)
  • 将LaunchScreen延迟1秒钟。

如果你想做更好的事情,你也可以用同样的方法扩展当前的RunLoop:

Swift 4:

RunLoop.current.run(until: Date(timeIntervalSinceNow: 1))

答案 16 :(得分:1)

这有效......

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

答案 17 :(得分:1)

将您的default.png放在UIImageView全屏幕中,作为主视图顶部的子视图,从而覆盖其他UI。设置一个计时器,在x秒(可能带有效果)后立即显示你的应用程序。

答案 18 :(得分:0)

继续项目名称。然后右键单击/属性/应用程序选项卡。 查找&#34;查看应用程序事件&#34;靠近Slash形式的组合框。 在myApplication Class:

中复制此代码
        Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
              System.Threading.Thread.Sleep(3000) ' or other time
        End Sub

答案 19 :(得分:0)

您可以创建自己的视图并在应用程序启动时显示它并使用计时器隐藏它。请避免延迟应用程序启动作为其坏主意

答案 20 :(得分:0)

在Swift 4.2中

默认启动时间后延迟1秒...

Thread.sleep(forTimeInterval: 1)

答案 21 :(得分:-1)

您可以在AppDelegate didFinishLaunchingWithOptions方法中简单地指定要休眠的秒数。

或者使用另一个ImageView来自定义启动画面。

我在以下链接中查看后者的详细信息:

Splash Screen Problem