How to add a 3D Touch menu to an app in Swift

OneSwift - iOS Tips Based On Swift

What I bring to you today is to add a 3D Touch menu to the application, so that users can quickly access certain pages on the home page. Taking OneDay as an example, through 3D Touch, users can quickly choose to enter the add page, setting page, archive page, and home page.

1. Create a custom 3D Touch menu

In AppDelegate, didFinishLaunchingWithOptionswe add the following code to add the button.

//添加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]

The icon of the button uses the icon image of the system, and other patterns can refer to this link.

3DTouch Xcode native icon icon pattern preview

2. Add a response event for each button

Then we add a response event for each button, because my four buttons just go to a fixed page, so the page jump can be realized in response to the event.

Bind the event function of the button:

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

The specific code of the function:

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
}

I used it here performSegue, so I Main.storyboardwill bind an ID to each jump in .

The follow-up will supplement how to customize the icon and how to implement 3D Touch in the page. Welcome to follow the follow-up updates of OneSwift.

GitHub:OneSwift - iOS Tips Based On Swift

Weibo : xDEHANG

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325417231&siteId=291194637