Swift如何给应用添加3D Touch菜单

OneSwift - iOS Tips Based On Swiftgit

今天为你们带来的是给应用添加3D Touch菜单,这样能够方便用户在首页便可快速访问某些页面。 以OneDay为例,经过3D Touch用户能够快速选择进入到添加页面、设置页面、归档页面、首页。github

1、建立自定义的3D Touch菜单

AppDelegatedidFinishLaunchingWithOptions中,咱们添加下列代码,来实现按钮的添加。api

//添加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图片,其余图样能够参考这个连接。bash

3DTouch Xcode原生图标icon图样预览app

2、为每一个按钮添加响应事件

接着咱们为每一个按钮添加响应事件,由于个人四个按钮恰好都到一个固定页面,因此响应事件实现页面的跳转便可。ide

绑定按钮的事件函数:函数

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
        let handledShortCutItem = handleShortCutItem(shortcutItem: shortcutItem)
        completionHandler(handledShortCutItem)
    }
复制代码

函数的具体代码:ui

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。spa

后续将补充介绍如何自定义icon、如何在页面内实现3D Touch,欢迎关注OneSwift的后续更新。3d

GitHub:OneSwift - iOS Tips Based On Swift

微博:xDEHANG

相关文章
相关标签/搜索