OS --- display the Splash page with scheduled tasks in Swift

Source: Internet
Author: User

OS --- display the Splash page with scheduled tasks in Swift

We use Swift to display the Splash page, but it is not enough to only display the Splash page. We need to display the Splash page for 2 seconds and then jump to the next page. Therefore, we need to implement the scheduled task function. In Android, we use the system function postDelayed to implement this function. In IOS, we need to use GCD.

First, let's take a look at GCD. We know that for mobile phones, multi-core CPUs are used more and more, so the real multi-task is becoming a reality, because each CPU core can execute separate tasks independently. GCD is a technology introduced to make it easier for programmers to use multi-core CPUs. GCD is called Grand Central Dispath in English and is an underlying c api. GCD creates an optimal thread pool based on multi-core CPU and hardware features. application programmers only need to submit tasks to GCD and specify the attributes of these tasks, such as synchronization, Asynchronization, and latency, then GCD will arrange these tasks to the appropriate threads for execution.

There is a dispath_after method in GCD, which can implement the function of executing a task at a delay. We can use this function to display the Splash page for 2 seconds and then jump to other pages.

To achieve delayed jump of the Splash page, we need to first define a target page for the Splash jump. Here is the application introduction page we have defined.

Note: In fact, when the Splash page is out of date, the system first checks whether the application is running for the first time. If the application is running for the first time, the Application Introduction page is displayed. If the application is not running for the first time and has been logged on, you can directly go to the application homepage. If you have not logged on to the application for the first time, you can go to the logon page. The logon page usually has a registration link to guide new users to register. Here, we do not consider these business logic for the time being, but directly go to the Application Introduction page after the Splash page is flashed.

First, we define the application overview page base class WKYAppTourView. swift in WkyLib. There is only one Label here, just to have a jump to the target page.

 

import UIKitpublic class WKYAppTourView{    public init(rootView: UIView) {        let testLabel = UILabel(frame: CGRectMake(0.0, 0.0, 120.0, 240.0))        testLabel.backgroundColor = UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0)        testLabel.text = Hello!        rootView.addSubview(testLabel)    }}

 

Define WKYAppTourViewController. swift

 

import UIKitpublic class WKYAppTourViewController: UIViewController{    override public func viewDidLoad() {        super.viewDidLoad()        let rootView = self.view    }        override public func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }        public var appTourView: WKYAppTourView?}

 

Define the Child class JYSAppTourView. swift of WKYAppTourView in the WkgJys project.

 

import UIKitimport WkyLibclass JYSAppTourView: WKYAppTourView{    override init(rootView: UIView) {        super.init(rootView: rootView)    }}

 

Define the Child class JYSAppTourViewController of WKYAppTourViewController

 

import UIKitimport WkyLibclass JYSAppTourViewController: WKYAppTourViewController{    override func viewDidLoad() {        super.viewDidLoad()        appTourView = JYSAppTourView(rootView: self.view)    }    }
After writing the above Code, run AppDelegate. the ViewController started in the application method in swift is replaced with the JYSAppTourViewController class (this class is only used to test whether the target page can be displayed normally and we will change it back to the Splash page later). The Code is as follows:

 

 

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {    // Override point for customization after application launch.    window = UIWindow(frame: UIScreen.mainScreen().bounds)    //window?.rootViewController = JYSAppSplashViewController()    window?.rootViewController = JYSAppTourViewController()    window?.makeKeyAndVisible()    return true}
At this time, run the WkgJys project. If there is a red-black Hello text, it proves that the above Code is okay, and then you can proceed down.

 

Start ViewController in AppDelegate. swift and change it back to JYSAppSplashViewController.

Modify the JYSAppSplashViewController. viewDidAppear method and add the delayed task code using GCD technology, as shown below:

 

override func viewDidAppear(animated: Bool) {    super.viewDidAppear(animated)    // delay task    let delayInSeconds = 5.0    let delayInNanoSeconds = dispatch_time(DISPATCH_TIME_NOW, Int64(delayInSeconds * Double(NSEC_PER_SEC)))    let currentQueue = dispatch_get_main_queue()    dispatch_after(delayInNanoSeconds, currentQueue, {        //self.changeRootViewController()        println(Delay in (delayInSeconds) seconds)    })}

 

Run the WkgJys project. After the project is started for 10 seconds, the Delay in 10 seconds text will be printed on the console. Note that we need to use the main thread to execute UI-related operations, so we call the dispatch_get_main_queue () method.

Now you can start JYSAppTourViewController in a delayed task. In general, ViewController is switched using Storyboard's seue mode. Another method is to use NavigationController to control the switchover. Because we do not use Storyboard, we cannot use the Storyboard method, when using NavigationController, there is a navigation bar on the top of the interface, which is in conflict with our full screen display. Therefore, we need to use other methods to achieve this.

First, we need to move the code of the delayed task from the viewDidLoad method to the viewDidAppeare method. The Code is as follows:

 

func changeRootViewController() {    let appTourViewController = JYSAppTourViewController()    self.dismissViewControllerAnimated(true, completion: nil)    self.view.window?.rootViewController = appTourViewController    self.view.window?.makeKeyAndVisible()}

 

Run the WkgJys project in this way. You can see that the Splash page is displayed for 5 seconds, and then switch to the temporary interface of the red and black text.

 

The implementation of the Splash page is basically complete. In the next section, we will return to the Android system and describe how to implement the Application Introduction page for one screen and one screen sliding display. Here, we also talked about how to identify whether an application is running for the first time and how information is stored persistently.

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.