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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/101675.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 程序员需要学数电吗(手机费电快有什么方法)

    目录????0.前言:????1.常见进制介绍:????(1)十进制:①示例:②可知一个任意多位的十进制数D均可展开为如下形式:③拓展——若以N取代式中的10,即可得到多位任意进制(N进制)数展开式的普遍形式:????(2)二进制:①根据N进制数展开的普遍形式可得任意一个二进制数均可展开为:![在这里插入图片描述](https://img-blog.csdnimg.cn/b7a289fac3d54c6f8ef071849945ca92.png)②并可利用上式计算出任一二进制数所表达的十进制数的大小:???

    2022年4月11日
    45
  • 2022年1月 idea最新激活码[最新免费获取]

    (2022年1月 idea最新激活码)2021最新分享一个能用的的激活码出来,希望能帮到需要激活的朋友。目前这个是能用的,但是用的人多了之后也会失效,会不定时更新的,大家持续关注此网站~https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~4K…

    2022年4月1日
    98
  • H3C交换机配置telnet

    H3C交换机配置telnet1、telnet配置方法(常用方法):<H3C>sysSystemView:returntoUserViewwithCtrl+Z.[H3C]telnetserverenable   //使能telnet服务[H3C]user-interfacevty0   //设置telnet[H3C-ui-vty0-4]authentication-modenone[H…

    2022年6月20日
    42
  • 环信集成常见错码

    环信集成常见错码

    2021年11月9日
    109
  • Spring获取request对象的几种方式[通俗易懂]

    Spring获取request对象的几种方式[通俗易懂]参考文章:在SpringMVC中获取request对象的几种方式Springboot获取request和response使用Springboot,我们很多时候直接使用@PathVariable、@RequestParam、@Param来获取参数,但是偶尔还是要用到request和response,怎么获取呢?也很方便,有三种方式可以获取,任选其一就行。1、通过静态方法…

    2022年5月18日
    53
  • java 流媒体服务器搭建_搭建流媒体服务器(1)

    java 流媒体服务器搭建_搭建流媒体服务器(1)一、前语本文纂写时间是2018年12月17日,所描述的软件WowzaMediaServer此时已经出了4或更高,但是2.2.2提供的功能已经是Goodenoughforme.如果发现4足够更好,后面文章也许会再续。本文试图描述一个在WindowsServer2012上安装了WowzaMediaServerv2.2.2流媒体服务的事件。最终会另服务器提供给外部适当的Server和…

    2022年5月16日
    38

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号