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

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

概述本文将演示3DTouch三维触摸手势的使用。 双卡双待的iPhone XR中,苹果取消了3DTouch功能。 在项目导航区,打开应用程序的代理文件【AppDelegate.swift】 当三维触摸手势被触发时,将在应用程序的图标位置显示一个菜单列表, 现在从创建这个菜单列表开始。 1 import UIKit 2 3 @UIApplicationMain 4 class AppDe

本文将演示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 true49     }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】返回系统界面。

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

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

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

总结

以上是内存溢出为你收集整理的[Swift通天遁地]三、手势与图表-(4)3DTouch功能在项目中的应用全部内容,希望文章能够帮你解决[Swift通天遁地]三、手势与图表-(4)3DTouch功能在项目中的应用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1021200.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存