您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

Swift 實作:用 GCD 延時任務讓 Splash 頁面停留兩秒後跳轉

編輯:iOS 開發綜合

用 Swift 畫出 Splash 頁面只是第一步,真正要做的是讓它停留約兩秒,再自動帶使用者進入下一個畫面。Android 上我們用 postDelayed 就能搞定,iOS 這邊則要借助 GCD。

先認識 GCD

手機的多核心 CPU 愈來愈普及,真正的多工也才成為可能——每個核心都能獨立跑各自的任務。GCD(Grand Central Dispatch)就是為了讓開發者更容易用上多核心而生的底層 C API。它會依據硬體特性建立最合適的執行緒池,開發者只需要把任務交出去,並標明同步、非同步、延時等屬性,剩下的排程由 GCD 統一處理。

其中 dispatch_after 正好能滿足「延時執行」的需求。

一、準備目標頁面

先在 WkyLib 中定義一個介紹頁的基底類別。這裡只放一個 Label,目的是先有個可跳轉的落點:

import UIKit

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

再定義對應的 view controller:

import UIKit

public class WKYAppTourViewController: UIViewController {
    override public func viewDidLoad() {
        super.viewDidLoad()
        let rootView = self.view
    }

    override public func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    public var appTourView: WKYAppTourView?
}

接著在實際工程 WkgJys 中繼承出子類別:

import UIKit
import WkyLib

class JYSAppTourView: WKYAppTourView {
    override init(rootView: UIView) {
        super.init(rootView: rootView)
    }
}

class JYSAppTourViewController: WKYAppTourViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        appTourView = JYSAppTourView(rootView: self.view)
    }
}

二、先驗證目標頁面

把 AppDelegate 啟動時的 rootViewController 暫時換成 JYSAppTourViewController,先確認畫面能正常顯示:

func application(application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    window = UIWindow(frame: UIScreen.mainScreen().bounds)
    // window?.rootViewController = JYSAppSplashViewController()
    window?.rootViewController = JYSAppTourViewController()
    window?.makeKeyAndVisible()
    return true
}

執行後如果看到紅底黑字的 Hello,代表這一段沒問題,可以繼續往下做。接著把 rootViewController 改回 JYSAppSplashViewController。

三、加入延時任務

在 viewDidAppear 中使用 GCD 的延時寫法。這裡刻意把延時設成 5 秒,方便測試:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

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

這裡有一個重點:因為接下來要操作 UI,所以必須指定主佇列(dispatch_get_main_queue()),背景佇列碰 UI 會直接出問題。

四、在延時任務中切換頁面

一般切換 view controller 會用 Storyboard 的 segue,或用 UINavigationController 的 push。但這個專案沒有用 Storyboard,而 NavigationController 頂部那條導航列又與我們想要的全螢幕 Splash 衝突,因此改用直接替換 window 的 rootViewController:

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

把 viewDidAppear 裡被註解掉的那行改回呼叫 changeRootViewController(),執行後就會看到 Splash 停留五秒,然後切換到紅底黑字的臨時畫面。

補充:真實專案裡還要想的事

實際產品中,Splash 消失後通常會先判斷是否為首次啟動:第一次啟動導向介紹頁;非首次且已登入直接進首頁;非首次但未登入則進登入頁,並在登入頁附上註冊入口。本文為了聚焦在延時跳轉,暫時略過這些判斷。

另外,把延時寫在 viewDidAppear 而非 viewDidLoad 是刻意的——viewDidLoad 可能在畫面尚未出現時就被呼叫,容易讓倒數計時提前結束。若之後要做真正的倒數動畫,記得在 view 消失時把計時器取消掉,避免背景仍持續觸發。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved