xcode 在启动屏幕中执行代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27642016/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:24:41  来源:igfitidea点击:

Execute code in Launch Screen

iosxcodeuser-interfacexib

提问by Dmitriy

Xcode allows to create launch screen in .xibfiles via Interface Builder. Is it possible to execute some code with the xib, just like in usual view controllers? It would be great if we can set different text/images/etc while app launching.

Xcode 允许.xib通过 Interface Builder在文件中创建启动屏幕。是否可以使用 xib 执行一些代码,就像在通常的视图控制器中一样?如果我们可以在应用程序启动时设置不同的文本/图像/等,那就太好了。

回答by Midhun MP

No, it's not possible.

不,这不可能。

When launch screen is being displayed your app will be in loading state.

当显示启动屏幕时,您的应用程序将处于加载状态。

Even the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptionswill not be completely executed while the launch screen is displayed.

即使在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions显示启动屏幕时也不会完全执行。

So it's clear that, you don't have any access to your app and so at this point you can't execute any code.

很明显,您无权访问您的应用程序,因此此时您无法执行任何代码。

回答by RainCast

I was trying to do the same thing here. :)

我试图在这里做同样的事情。:)

I really liked some of the apps, in which they do a little dynamic greeting text and image each time the app is launched, such as "You look good today!", "Today is Friday, a wonderful day", etc, which is very cute.

我真的很喜欢一些应用程序,它们在每次启动应用程序时都会做一些动态的问候文字和图像,例如“你今天看起来不错!”、“今天是星期五,美好的一天”等,这是非常可爱。

I did some search, below is how to do it: (My code is XCode 7, with a launchscreen.xib file)

我做了一些搜索,下面是如何做:(我的代码是 XCode 7,带有一个 launchscreen.xib 文件)

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var customizedLaunchScreenView: UIView?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        // Override point for customization after application launch.
        application.statusBarHidden = true

        // customized launch screen
        if let window = self.window {
            self.customizedLaunchScreenView = UIView(frame: window.bounds)
            self.customizedLaunchScreenView?.backgroundColor = UIColor.greenColor()

            self.window?.makeKeyAndVisible()
            self.window?.addSubview(self.customizedLaunchScreenView!)
            self.window?.bringSubviewToFront(self.customizedLaunchScreenView!)
            UIView.animateWithDuration(1, delay: 2, options: .CurveEaseOut,
                                       animations: { () -> Void in
                                        self.customizedLaunchScreenView?.alpha = 0 },
                                       completion: { _ in
                                        self.customizedLaunchScreenView?.removeFromSuperview() })
        }

        return true
    }

// other stuff ...

}

Just do what ever you wanted to show, text, images, animations, etc. inside the customizedLaunchScreenViewhere.

在这里的customizedLaunchScreenView 中执行您想显示的任何内容,文本、图像、动画等。

At the end of the launching, just fade out this customized UIView using alpha value change, then remove it completely.

在启动结束时,只需使用 alpha 值更改淡出此自定义 UIView,然后将其完全删除。

How cool is that? I absolutely love it!

多么酷啊?我绝对喜欢它!

Hope it helps.

希望能帮助到你。

回答by Bilal Ahmad

I was also trying to achieve this. I tried the following, it gives a delay of couple of seconds but works for me.

我也在努力实现这一目标。我尝试了以下操作,它会延迟几秒钟,但对我有用。

  • Create and set a launch screen in project settings.
  • Create a view controller with custom class (SplashViewController) and set it your starting view controller in storyboard.
  • Add a container view in it and set it to full screen.
  • Set embedded segue to a Storyboard Reference.
  • Select Storyboard Reference and set launch screen in StoryBoard property from Attribute inspector.
  • Do whatever you want in SplashViewController (play animation or session check etc) and perform segue when done.
  • 在项目设置中创建和设置启动屏幕。
  • 创建一个带有自定义类 (SplashViewController) 的视图控制器,并将其设置为故事板中的起始视图控制器。
  • 在其中添加一个容器视图并将其设置为全屏。
  • 将嵌入的 segue 设置为 Storyboard Reference。
  • 从属性检查器中选择 Storyboard Reference 并在 StoryBoard 属性中设置启动屏幕。
  • 在 SplashViewController 中做任何你想做的事情(播放动画或会话检查等)并在完成后执行 segue。

Hope it helps!

希望能帮助到你!