【问题标题】:Displaying splash screen for longer than default seconds显示启动画面的时间超过默认秒数
【发布时间】:2011-08-02 20:30:39
【问题描述】:

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

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

【问题讨论】:

标签: ios objective-c splash-screen


【解决方案1】:

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

您可以添加一个新的视图控制器,它将在应用程序didFinishLoading 中显示default.png

这样您可以将default.png 显示得更长一点。

只有在加载数据时才应显示default.png,这可能需要一些时间。 正如应用商店指南所述,您不应在必要时延迟启动。

【讨论】:

  • 这样做,不要像其他答案中建议的那样让您的应用进入睡眠状态。 +1 正确答案。
  • 如果我让我的应用进入睡眠状态..那么苹果会拒绝我的应用吗?
  • 或者最好不要这样做。它违背了 HIG,就 UX 而言简直是愚蠢的。
  • 我知道这是另一种解决方法,但是如何将大尺寸的图像/资源放在应用程序中而不使用它。 iOS 会尝试加载它,加载应用程序需要更多时间。因此,有更多的时间用于启动画面。
  • 延迟启动您的应用可能会导致您的应用因加载时间过长而被看门狗杀死。所以永远不要延迟启动。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      你也可以使用NSThread:

      [NSThread sleepForTimeInterval:(NSTimeInterval)];
      

      你可以把这段代码放到applicationDidFinishLaunching方法的第一行。

      例如,显示 default.png 5 秒。

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

      【讨论】:

      • 简直完美。谢谢人。 :)
      • 延迟启动您的应用可能会导致您的应用因加载时间过长而被看门狗杀死。所以永远不要延迟启动。
      • 天啊!请不要使用这种方法,这简直太糟糕了,您正在阻止应用程序主线程冻结应用程序,正如@rckoenes 所解释的那样,该应用程序可以被杀死,违反任何苹果准则并且绝对丑陋。而是实现您的 VC 并显示它
      • 天啊!这太可怕了。永远不要这样做。请。它可能会在你的应用程序启动之前就被杀死。不要这样做。
      【解决方案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];
      }
      

      【讨论】:

        【解决方案6】:

        您可以使用以下代码:

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

        【讨论】:

        • 另外,要使图像适合任何尺寸的屏幕,请查看:drawInRect:self.view.bounds
        【解决方案7】:

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

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

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

        Splash Screen Problem

        【讨论】:

        • 延迟启动您的应用可能会导致您的应用因加载时间过长而被看门狗杀死。所以永远不要延迟启动。
        【解决方案8】:

        sleep(5.0)

        - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions会显示5秒闪屏

        【讨论】:

        • 延迟启动您的应用可能会导致您的应用因加载时间过长而被看门狗杀死。所以永远不要延迟启动。
        • 延迟 2 秒会有问题吗?
        【解决方案9】:

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

        [NSThread sleepForTimeInterval:5.0];
        

        它将停止启动画面 5.0 秒。

        【讨论】:

        • 延迟启动您的应用可能会导致您的应用因加载时间过长而被看门狗杀死。所以永远不要延迟启动。
        • 在 Swift 中的 didFinishLaunchingWithOptions:AppDelegate 类的委托方法中添加以下代码:Thread.sleep(forTimeInterval: 5.0)
        • 2018 年的评论。我需要 2 秒的延迟,我有一个来自 xcassets 的启动图像的启动屏幕,而不是来自 xib 或故事板,所以显示一个服务的屏幕很麻烦作为“扩展启动屏幕”,因为您必须确定手机的类型,并且您的确切显示应该与显示的启动图像匹配。所以我想如果我只需要 1 秒钟的睡眠,这不会那么糟糕。只需.... 1 秒。
        【解决方案10】:

        这行得通...

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

        【讨论】:

          【解决方案11】:

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

          在 AppDelegateod 中的 e didFinishLaunchingWithOptionsmeth 中添加以下语句

          NSThread.sleepForTimeInterval(3)
          

          这里,时间可以根据你的要求过去。

          SWIFT 5

          Thread.sleep(forTimeInterval: 3)
          

          【讨论】:

          • 和我之前说的一样:延迟启动你的应用可能会导致你的应用被看门狗杀死,因为加载时间过长。所以永远不要延迟启动。
          【解决方案12】:

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

          【讨论】:

            【解决方案13】:

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

            【讨论】:

              【解决方案14】:

              只要继续项目名称。然后右键单击/属性/应用程序选项卡。 在 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
              

              【讨论】:

                【解决方案15】:

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

                【讨论】:

                  【解决方案16】:

                  这是超级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]];
                  

                  【讨论】:

                  • 不错的一个。我会使用这个解决方案,但它需要我将一堆初始化的东西从默认线程(主线程)移动到另一个线程,它可能会产生新的意外行为来解决。
                  • @Daniel Storm。这会是苹果拒绝应用的原因吗?
                  • @zulkarnainshah 是的,因为它会导致监视应用程序的看门狗杀死您的应用程序。哪个苹果会崩溃并拒绝您的应用程序。您永远不应该延迟应用的启动,永远!!!
                  • 延迟不是一个好主意,你会冻结你的应用程序一秒钟。应该有更好的办法。
                  • @YucelBayram 完全同意。应该在 root 上镜像启动屏幕,并在准备好后推送/替换到初始屏幕。
                  【解决方案17】:

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

                  【讨论】:

                    【解决方案18】:

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

                    【讨论】:

                      【解决方案19】:

                      斯威夫特 3

                      这是通过在您指定的任何时间呈现启动控制器然后将其移除并显示您的正常 rootViewController 以安全的方式实现的。

                      1. 首先在 LaunchingScreen.storyboard 中为您的控制器提供一个 StoryBoard 标识符,比如说“splashController”
                      2. 在 Main.storyboard 中为您的初始 viewController 提供一个 StoryBoard 标识符,比如说“initController”。 - 这可能是导航栏或标签栏等...-

                      在 AppDelegate 中,您可以创建以下 2 个方法:

                      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.

                      @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 中。

                      你准备好了……

                      【讨论】:

                        【解决方案20】:

                        这里最简单的解决方案是在AppDelegate 类中将sleep() 添加到didFinishLaunchingWithOptions 方法。

                        斯威夫特 4:

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

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

                        斯威夫特 4:

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

                        【讨论】:

                        • 好的解决方案 - 但它们之间有什么区别??? (从更深入的角度来看)?
                        【解决方案21】:

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

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

                        【讨论】:

                          【解决方案22】:

                          在 Swift 4.2 中

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

                          Thread.sleep(forTimeInterval: 1)
                          

                          【讨论】:

                            猜你喜欢
                            • 1970-01-01
                            • 1970-01-01
                            • 2013-04-17
                            • 1970-01-01
                            • 2015-12-11
                            • 1970-01-01
                            • 1970-01-01
                            • 2021-10-23
                            相关资源
                            最近更新 更多