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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • javaweb教务管理系统源码_java做一个学生管理系统

    javaweb教务管理系统源码_java做一个学生管理系统刚学完javaweb,做个项目练手与巩固所学的知识,同时分享自己写的这个教务管理系统,同样也是为自己记下笔记。为以后复习有帮助。该系统没有用到框架的知识,一共花了7天写出来。所以非常适合javaweb初学者教务管理系统javaweb项目运行环境:window系统,ApacheTomcatv7.0.84、JDK1.8开发环境:J2EEeclipse、navicatformysql运用的技术…

    2022年10月16日
    0
  • 三阶魔方还原公式_三阶魔方顶层小鱼口诀

    三阶魔方还原公式_三阶魔方顶层小鱼口诀1.第二层棱块归位:2.顶层十字3.顶层棱中间块归位这一步的目的是使顶层的4个棱中间块全部归位。转动顶层(U),若可以使一个棱中间块归位(如下图左,这里以[红-黄]块为例),而其他3个都

    2022年8月6日
    12
  • oracle date_trunc_oracle中truncate用法

    oracle date_trunc_oracle中truncate用法PostgreSQL与Oracle对应的函数一、对应的函数1.sysdateoraclepgsqlsysdatecurrent_date、current_timestampnvlcoalescetruncdate_trunc(text,timestamp)…文章rayner2018-01-05842浏览量Oracle日期显示问题以及trunc方法的使用我们先来假设这么一个场景,只要输入一个…

    2022年10月25日
    0
  • verilog流水线设计代码_十进制BCD译码器的verilog

    verilog流水线设计代码_十进制BCD译码器的verilog流水线概述如下图为工厂流水线,工厂流水线就是将一个工作(比如生产一个产品)分成多个细分工作,在生产流水线上由多个不同的人分步完成。这个待完成的产品在流水线上一级一级往下传递。比如完成一个产品,需要8道工序,每道工序需要10s,那么流水线启动后,不间断工作的话,第一个产品虽然要80s才完成,但是接下来每10s就能产出一个产品。使得速度大大提高。当然这也增加了人员等资源的付出。对于电路的流水线…

    2022年8月14日
    5
  • mediumtext java_加快此查询(加入mediumtext字段)「建议收藏」

    mediumtext java_加快此查询(加入mediumtext字段)「建议收藏」我有这个问题SELECTt.name,t.userid,t.date,t.cat_id,t.forum_id,t.reply,t.hidden,t.moderated,t.sticky,t.statut,t.poll,t.helpful,t.del,t_data.message,user.nameASauthor_name,user.levelASauthor_level,user….

    2022年6月11日
    54
  • MFC界面库BCGControlBar的介绍

    MFC界面库BCGControlBar的介绍英文原文:http://www.bcgsoft.com/bcgcontrolbarpro.htmBCGControlBar是MFC的一个扩展库其英文全称是”BusinessComponentsGalleryControlBar”,它允许你去创建像完全自定义的像MicrosoftOffice2000/XP/2003/2007/2010/2013and VisualStudio的界

    2022年7月14日
    30

发表回复

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

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