为有牺牲多壮志,敢教日月换新天。

[Swift通天遁地]三、手势与图表-(4)3DTouch功能在项目中的应用

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10210693.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

目录:[Swift]通天遁地Swift

本文将演示3DTouch三维触摸手势的使用。

双卡双待的iPhone XR中,苹果取消了3DTouch功能。

在项目导航区,打开应用程序的代理文件【AppDelegate.swift】

当三维触摸手势被触发时,将在应用程序的图标位置显示一个菜单列表,

现在从创建这个菜单列表开始。

 1 import UIKit
 2 
 3 @UIApplicationMain
 4 class AppDelegate: UIResponder, UIApplicationDelegate
 5 {
 6     var window: UIWindow?
 7 
 8     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
 9     {
10         // Override point for customization after application launch.
11         
12         //初始化一个应用程序快捷图标,
13         //并设置图标的类型为添加。
14         let addEventIcon = UIApplicationShortcutIcon(type: .add)
15         //初始化另一个应用程序快捷图标,
16         //并设置图标的类型为自定义图片。
17         let unlockEventIcon = UIApplicationShortcutIcon(templateImageName: "unlockEvent")
18         //初始化另一个应用程序快捷图标,
19         //并设置图标的类型为自定义图片。
20         let listEventIcon = UIApplicationShortcutIcon(templateImageName: "listEvent")
21         
22         //创建菜单列表中的快捷条目,并设置相关参数。
23         let addEvent = UIApplicationShortcutItem(type: "com.coolketang.addMember", //条目的类型
24                                                  localizedTitle: "Add", //本地化标题
25                                                  localizedSubtitle: "Add Member",//子标题
26                                                  icon: addEventIcon, //图标
27                                                  userInfo: nil)//用户数据
28         
29         //创建菜单列表中的快捷条目,并设置相关参数。
30         let unlockEvent = UIApplicationShortcutItem(type: "com.coolketang.unlockMember", //条目的类型
31                                                     localizedTitle: "Unlock",//本地化标题
32                                                     localizedSubtitle: "Unlock Member",//子标题
33                                                     icon: unlockEventIcon, //图标
34                                                     userInfo: nil)//用户数据
35         
36         //创建菜单列表中的快捷条目,并设置相关参数。
37         let listEvent = UIApplicationShortcutItem(type: "com.coolketang.memberList", //条目的类型
38                                                     localizedTitle: "List", //本地化标题
39                                                     localizedSubtitle: "Members List",//子标题
40                                                     icon: listEventIcon,//图标
41                                                     userInfo: nil)//用户数据
42         
43         //将三个快捷条目添加到数组中
44         let shortCutItems = [addEvent, unlockEvent, listEvent]
45         //然后设置应用程序对象的快捷列表,在快捷列表中包含三个快捷条目
46         application.shortcutItems = shortCutItems;
47         
48         return true
49     }
50 
51     //添加一个方法,用来响应快捷条目被点击时的事件。
52     func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
53         //根据返回的快捷条目的类型,判断用户需要使用哪个功能。
54 
55         //当用户点击第一个条目时
56         if shortcutItem.type == "com.coolketang.addMember"
57         {
58             //在控制台输出对应的日志信息
59             print("Navigate to page for adding memeber.")
60         }
61          //当用户点击第二个条目时
62         else if shortcutItem.type == "com.coolketang.unlockMember"
63         {
64             //在控制台输出对应的日志信息
65             print("Navigate to page for unlocking memeber.")
66         }
67          //当用户点击第三个条目时
68         else if shortcutItem.type == "com.coolketang.memberList"
69         {
70             //在控制台输出对应的日志信息
71             print("Navigate to list of memebers.")
72         }
73     }
74 
75     func applicationWillResignActive(_ application: UIApplication) {
76         // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
77         // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
78     }
79 
80     func applicationDidEnterBackground(_ application: UIApplication) {
81         // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
82         // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
83     }
84 
85     func applicationWillEnterForeground(_ application: UIApplication) {
86         // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
87     }
88 
89     func applicationDidBecomeActive(_ application: UIApplication) {
90         // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
91     }
92 
93     func applicationWillTerminate(_ application: UIApplication) {
94         // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
95     }
96 }

切换到真机环境,进行三维触摸手势的测试。

按下【Command】+【Shif】+【H】返回系统界面。

稍微用力并长按应用程序的快捷图标,以打开快捷条目列表。

在打开的快捷条目列表中,左侧时条目的图标,右侧是条目的标题。

点击一个条目,将返回应用程序,并在控制台输出相应的内容。

posted @ 2019-01-02 19:29  为敢技术  阅读(192)  评论(0编辑  收藏  举报