★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-erhplump-gu.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
目錄:[Swift]通天遁地Swiftgit
本文將演示3DTouch三維觸摸手勢的使用。github
雙卡雙待的iPhone XR中,蘋果取消了3DTouch功能。swift
在項目導航區,打開應用程序的代理文件【AppDelegate.swift】api
當三維觸摸手勢被觸發時,將在應用程序的圖標位置顯示一個菜單列表,數組
如今從建立這個菜單列表開始。微信
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 }
切換到真機環境,進行三維觸摸手勢的測試。app
按下【Command】+【Shif】+【H】返回系統界面。ide
稍微用力並長按應用程序的快捷圖標,以打開快捷條目列表。post
在打開的快捷條目列表中,左側時條目的圖標,右側是條目的標題。
點擊一個條目,將返回應用程序,並在控制檯輸出相應的內容。