xcode 来自 Storyboard 的 iOS Swift 启动画面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31114418/
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
iOS Swift splash screen from Storyboard
提问by JibW
In my iPhone app need to display a splash screen (displays company logo and some info) for 2-3 seconds before load the first page.
在我的 iPhone 应用程序中,需要在加载第一页之前显示启动画面(显示公司徽标和一些信息)2-3 秒。
Also the app needs to decide which page should load as the first page in here (according the 'initial setup completion' level by the user).
此外,应用程序需要决定哪个页面应作为此处的第一页加载(根据用户的“初始设置完成”级别)。
I am using 'Swift' as the programing language and 'Universal StoryBoard' to design interfaces...
我使用“Swift”作为编程语言,使用“Universal StoryBoard”来设计界面……
I have seleted the Main.storyboard as the Launch Screen File. In the ViewController class have implemented following logic
我已选择 Main.storyboard 作为启动屏幕文件。在 ViewController 类中实现了以下逻辑
override func viewDidAppear(animated: Bool) {
NSLog("Before sleep...")
sleep(2)
NSLog("After sleep...")
self.controllNavigation()
}
func controllNavigation() -> Void {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var nextViewController
if (Condition1)
{
nextViewController = storyBoard.instantiateViewControllerWithIdentifier("MainMenu") as! MainMenuViewController
}
else
{
nextViewController = storyBoard.instantiateViewControllerWithIdentifier("UserSetup") as! UserSetupViewController
}
self.presentViewController(nextViewController, animated: true, completion: nil)
}
All works ok but While waiting with sleep(2)
, refresh page sort of a thing happens. I am not sure if this is the best way to do. Like to hear ideas. Thanks
一切正常,但在等待时sleep(2)
,刷新页面发生了一些事情。我不确定这是否是最好的方法。喜欢听想法。谢谢
回答by Kapil Choubisa
Use the delay in app delegate instead of viewController's viewDidAppear. Use as:
在应用程序委托中使用延迟而不是 viewController 的 viewDidAppear。用于:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
NSLog("Before Sleep");
sleep(2);
NSLog("After sleep");
return true
}
Doing this will allow your splash screen to stay till 2 seconds and then you can use following in your view controller:
这样做将使您的启动画面保持 2 秒,然后您可以在视图控制器中使用以下内容:
override func viewDidAppear(animated: Bool) {
self.controllNavigation()
}
Hope this helps :)
希望这可以帮助 :)
回答by David T
I came across similar answers which mentioned sleep()
, however it doesn't seem appropriate to use as it blocks the main thread.
我遇到了提到的类似答案sleep()
,但是它似乎不适合使用,因为它会阻塞主线程。
I came up with this solution if you are using the default LaunchScreen.storyboard in Xcode 7.2:
如果您在 Xcode 7.2 中使用默认 LaunchScreen.storyboard,我想出了这个解决方案:
func showLaunchScreen() {
let launchScreen = UIStoryboard(name: "LaunchScreen", bundle: nil)
let launchView = launchScreen.instantiateInitialViewController()
self.view.addSubview(launchView!.view)
let timeDelay = dispatch_time(DISPATCH_TIME_NOW, Int64(2 * Double(NSEC_PER_SEC)))
dispatch_after(timeDelay, dispatch_get_main_queue()) {
UIView.animateWithDuration(0.5, animations: { _ in
launchView?.view.alpha = 0.0
}) { _ in
launchView!.view.removeFromSuperview()
}
}
This will show the launch screen for another 2 seconds, then fade it out over 0.5 seconds. You can call you other function to load the 'default' VC in the completion handler.
这将再显示启动屏幕 2 秒,然后在 0.5 秒内淡出。您可以调用其他函数以在完成处理程序中加载“默认”VC。
回答by Jorge Sánchez
I have solved this issue with this:
我已经解决了这个问题:
On my project I set the desired storyboard of splash (in my case Splash.storyboard) as Default Screen -> Targets/ (your target)/ Info/ Launch Screen Interface File Base Name.
在我的项目中,我将所需的初始故事板(在我的情况下为 Splash.storyboard)设置为默认屏幕 -> 目标/(您的目标)/信息/启动屏幕界面文件基名。
When you've done that, you can insert your logic on method "func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {} " placed on AppDelegate.swift .
完成后,您可以在方法“func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {}”上插入逻辑,放在 AppDelegate.swift 上。
If you want to make clean code, you can insert all your navigation related methods on another Class, and call them from AppDelegate.swift .
如果你想编写干净的代码,你可以将所有与导航相关的方法插入另一个类,并从 AppDelegate.swift 调用它们。
With this solution, the splash is visible only the required time, and when it finish, you can navigate to the screen you need.
使用此解决方案,飞溅仅在所需的时间内可见,完成后,您可以导航到所需的屏幕。
回答by Xcodian Solangi
Swift 4 Update 100% working
Swift 4 更新 100% 工作
Just write one line of code
Thread.sleep(forTimeInterval: 3.0)
in the method of didfinishLauching....
in appdelegate
class.
在类Thread.sleep(forTimeInterval: 3.0)
的方法中写一行代码即可
。didfinishLauching....
appdelegate
Example
例子
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Thread.sleep(forTimeInterval: 3.0)
// Override point for customization after application launch.
return true
}