OS---在Swift中用定時任務顯示Splash頁面

來源:互聯網
上載者:User

OS---在Swift中用定時任務顯示Splash頁面

我們用Swift實現了Splash頁面的顯示,但是僅僅顯示Splash頁面是不夠的,我們需要顯示Splash頁面2秒,然後跳轉到下一個頁面,因此需要實現定時任務的功能。在Android中,我們採用了系統的函數postDelayed來實現這一功能,在IOS系統中,我們需要使用GCD。

首先我們簡單瞭解一下GCD。我們知道,對於手機而言,多核CPU用得越來越多,這樣真正的多任務就是益成為現實,因為每個CPU核都可以獨立地執行單獨的任務。GCD正是為了使程式員更方便地使用多核CPU而引入的技術。GCD的英文全稱為Grand Central Dispath,是一個底層C API。GCD會根據多核CPU和硬體特性,建立最佳的線程池,應用程式員只需向GCD提交任務,並規定這些任務的屬性,如同步、非同步、延時等,然後由GCD統一安排這些任務到合適的線程來執行。

在GCD中有一個dispath_after方法,可以實現延時執行一個任務的功能,我們可以使用該函數來實現Splash頁面顯示2秒,然後跳轉到其他頁面的功能。

為了實現Splash頁面的延時跳轉,我們需要首先定義一個Splash跳轉的目標頁面,這裡是我們定義的應用介紹頁面。

注意,實際上,當Splash頁面閃過時,會首先判斷應用是否是第一次運行,如果是第一次運行,則顯示應用介紹頁面,如果不是第一次運行且已經登入,則直接進入應用首頁,如果不是第一次運行且沒有登入,則進入登入頁面,通常登入頁面還有註冊連結,引導新使用者前往註冊。在這裡,我們暫時前不考慮這些商務邏輯,只是在Splash頁面閃過之後,直接進入應用介紹頁面。

首先,我們在WkyLib中定義應用介紹頁面基類WKYAppTourView.swift,這裡只有一個Label,只是為了有一個跳轉目標頁面而已

 

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

 

再定義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?}

 

在WkgJys工程中定義WKYAppTourView的子類JYSAppTourView.swift

 

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

 

定義WKYAppTourViewController的子類JYSAppTourViewController

 

import UIKitimport WkyLibclass JYSAppTourViewController: WKYAppTourViewController{    override func viewDidLoad() {        super.viewDidLoad()        appTourView = JYSAppTourView(rootView: self.view)    }    }
寫完上述代碼後,將AppDelegate.swift中的application方法中啟動的ViewController換成JYSAppTourViewController類(僅用於測試我們跳轉目標頁面能夠正常顯示,稍後我們還將改回Splash頁面),代碼如下所示:

 

 

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}
這時運行WkgJys工程,如果出現紅底黑字的Hello文字,就證明上述代碼沒有問題了,就可以接著向下進行了。

 

首先將AppDelegate.swift中啟動ViewController改回JYSAppSplashViewController。

然後修改JYSAppSplashViewController.viewDidAppear方法,加入使用GCD技術的延時任務代碼,如下所示:

 

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

 

運行WkgJys工程,在工程啟動10秒之後,會在控制台上打出Delay in 10 seconds文字。這裡需要注意,由於我們要進行UI相關操作,因此需要使用主線程來執行,因此調用了dispatch_get_main_queue()方法。

好了,現在可以開始實現在延時任務裡啟動JYSAppTourViewController了。通常ViewController的切換是使用Storyboard的segue方式切換,另一種方式是用NavigationController來控制切換,由於我們不採用Storyboard,所以不能使用Storyboard相關方法,而使用NavigationController時,介面上部會有一個導航條,與我們的全螢幕顯示有矛盾,因此需要採用其他方式來實現。

首先,我們需要將延時任務的代碼從viewDidLoad方法移到viewDidAppeare方法中,代碼如下所示:

 

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

 

這樣運行一下WkgJys工程,應可以看到Splash頁面顯示5秒後,就切換到紅底黑字的臨時介面了。

 

Splash頁面實現就基本完成了,下一節我們將回到Android系統,講述怎樣實現一屏一屏滑動顯示的應用介紹頁面,在這裡,我們還會談到怎樣識別是否是應用第一次運行,以及資訊持久化儲存的方式。

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.