博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Swift如何给应用添加3D Touch菜单
阅读量:6879 次
发布时间:2019-06-27

本文共 3645 字,大约阅读时间需要 12 分钟。

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

一、创建自定义的3D Touch菜单

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

//添加icon 3d Touchlet 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图片,其他图样可以参考这个链接。

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

接着我们为每个按钮添加响应事件,因为我的四个按钮刚好都到一个固定页面,所以响应事件实现页面的跳转即可。

绑定按钮的事件函数:

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:

微博:

转载地址:http://xkfbl.baihongyu.com/

你可能感兴趣的文章
关于centerOS下修改网络连接
查看>>
牛客暑假多校第二场 K carpet
查看>>
Linux下chkconfig命令详解(转)
查看>>
EF中,保存实体报错:Validation failed for one or more entities. 如何知道具体错误在哪?...
查看>>
和积式
查看>>
你不能错过.net 并发解决方案
查看>>
[PHP] 超全局变量$_FILES上传文件
查看>>
linux如何添加telnet服务
查看>>
解决Windows对JDK默认版本切换问题
查看>>
HTML5本地存储localStorage与seesionStorage
查看>>
06笨小猴(1.9)
查看>>
UNIX网络编程——原始套接字的魔力【上】
查看>>
web应用开发技术(第二版)崔尚森第八章部分作业
查看>>
thinkCMF----列表页跳转
查看>>
VIM编辑器和VI编辑器的区别
查看>>
hdu 1693 : Eat the Trees 【插头dp 入门】
查看>>
nginx安装与fastdfs配置--阿里云
查看>>
wordpress通过代码禁用IE8, IE9,IE10等IE浏览器兼容视图模式(Compatibility View)
查看>>
This application failed to start because it could not find or load the Qt platform plugin "windows"
查看>>
CSS3展现精彩的动画效果 css3的动画属性
查看>>