Swift如何給應用新增3D Touch選單

xDEHANG發表於2019-02-26

OneSwift – iOS Tips Based On Swift

今天為大家帶來的是給應用新增3D Touch選單,這樣可以方便使用者在首頁即可快速訪問某些頁面。
以OneDay為例,通過3D Touch使用者可以快速選擇進入到新增頁面、設定頁面、歸檔頁面、首頁。

一、建立自定義的3D Touch選單

AppDelegatedidFinishLaunchingWithOptions中,我們新增下列程式碼,來實現按鈕的新增。

//新增icon 3d Touch
let firstItemIcon:UIApplicationShortcutIcon = UIApplicationShortcutIcon(type: .confirmation)
let firstItem = UIMutableApplicationShortcutItem(type: "1", localizedTitle: NSLocalizedString("Home", comment: "Home icon"), localizedSubtitle: nil, icon: firstItemIcon, userInfo: nil)

let firstItemIcon1:UIApplicationShortcutIcon = UIApplicationShortcutIcon(type: .taskCompleted)
let firstItem1 = UIMutableApplicationShortcutItem(type: "2", localizedTitle: NSLocalizedString("Archive ", comment: "Archive icon"), localizedSubtitle: nil, icon: firstItemIcon1, userInfo: nil)


let firstItemIcon2:UIApplicationShortcutIcon = UIApplicationShortcutIcon(type: .task)
let firstItem2 = UIMutableApplicationShortcutItem(type: "3", localizedTitle: NSLocalizedString("Setting", comment: "Setting icon"), localizedSubtitle: nil, icon: firstItemIcon2, userInfo: nil)


let firstItemIcon3:UIApplicationShortcutIcon = UIApplicationShortcutIcon(type: .add)
let firstItem3 = UIMutableApplicationShortcutItem(type: "4", localizedTitle: NSLocalizedString("Add", comment: "Add icon"), localizedSubtitle: nil, icon: firstItemIcon3, userInfo: nil)

application.shortcutItems = [firstItem,firstItem1,firstItem2,firstItem3]

複製程式碼

其中按鈕的icon使用系統的icon圖片,其他圖樣可以參考這個連結。

3DTouch Xcode原生圖示icon圖樣預覽

二、為每個按鈕新增響應事件

接著我們為每個按鈕新增響應事件,因為我的四個按鈕剛好都到一個固定頁面,所以響應事件實現頁面的跳轉即可。

繫結按鈕的事件函式:

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
        let handledShortCutItem = handleShortCutItem(shortcutItem: shortcutItem)
        completionHandler(handledShortCutItem)
    }
複製程式碼

函式的具體程式碼:

func handleShortCutItem(shortcutItem: UIApplicationShortcutItem) -> Bool {
    var handled = false

    if shortcutItem.type == "1" { //首頁

        let rootNavigationViewController = window!.rootViewController as? UINavigationController
        let rootViewController = rootNavigationViewController?.viewControllers.first as UIViewController?

        rootNavigationViewController?.popToRootViewController(animated: false)
        handled = true

    }
    if shortcutItem.type == "2" { //編輯

        let rootNavigationViewController = window!.rootViewController as? UINavigationController
        let rootViewController = rootNavigationViewController?.viewControllers.first as UIViewController?

        rootNavigationViewController?.popToRootViewController(animated: false)
        rootViewController?.performSegue(withIdentifier: "toArchive", sender: nil)
        handled = true

    }

    if shortcutItem.type == "3" { //設定

        let rootNavigationViewController = window!.rootViewController as? UINavigationController
        let rootViewController = rootNavigationViewController?.viewControllers.first as UIViewController?

        rootNavigationViewController?.popToRootViewController(animated: false)
        rootViewController?.performSegue(withIdentifier: "toSetting", sender: nil)
        handled = true

    }

    if shortcutItem.type == "4" { //編輯

        let rootNavigationViewController = window!.rootViewController as? UINavigationController
        let rootViewController = rootNavigationViewController?.viewControllers.first as UIViewController?

        rootNavigationViewController?.popToRootViewController(animated: false)
        rootViewController?.performSegue(withIdentifier: "addTodo", sender: nil)
        handled = true

    }

    return handled
}
複製程式碼

這裡我用到了performSegue,所以在Main.storyboard中會給每個跳轉繫結ID。

後續將補充介紹如何自定義icon、如何在頁面內實現3D Touch,歡迎關注OneSwift的後續更新。

GitHub:OneSwift – iOS Tips Based On Swift

微博:xDEHANG

相關文章