前言
3D Touch為用戶提供了全新維度上的交互,在支持3D Touch的設(shè)備上,用戶可以改變手指的按壓力度使設(shè)備做出不同的響應(yīng)。
3D Touch的主要功能:
1、Home Screen Quick Actions
2、Peek & Pop
3、Force Properties
下面介紹如何將3D Touch應(yīng)用到項目中。
一、Home Screen Quick Actions
這一功能是通過重按主屏幕的icon呼出一個菜單,快速定位到相應(yīng)的功能,有靜態(tài)集成和動態(tài)集成兩種方法,也可以將兩種方法混用。
靜態(tài)集成
靜態(tài)集成是在info.plist文件中添加相應(yīng)的鍵值對,如下:
屬性:
- UIApplicationShortcutItemTitle 標簽名,必須
- UIApplicationShortcutItemType 標簽類型,可用于判斷用戶點擊的標簽,必須
- UIApplicationShortcutItemSubtitle 副標題,可選
- UIApplicationShortcutItemIconType 使用系統(tǒng)圖標,可選
- UIApplicationShortcutItemIconFile 使用boundle或者image asset catalog中的圖片,35*35點,可選
動態(tài)集成
在func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
方法中添加:
if #available(iOS 9.0, *) {
//這里使用系統(tǒng)圖標
let shortcutIcon = UIApplicationShortcutIcon.init(type: UIApplicationShortcutIconType.search)
let shortcut1 = UIApplicationShortcutItem(type: "2", localizedTitle: "搜索", localizedSubtitle: nil, icon: shortcutIcon, userInfo: nil)
//這里使用自己的圖片,但由于不符合要求,在標簽中變成了一坨黑色的東西- -
let shortIcon2 = UIApplicationShortcutIcon.init(templateImageName: "lvtuicon")
let shortcut2 = UIApplicationShortcutItem(type: "3", localizedTitle: "分享", localizedSubtitle: nil, icon: shortIcon2, userInfo: nil)
UIApplication.shared.shortcutItems = [shortcut1, shortcut2]
} else {
// Fallback on earlier versions
}
處理點擊事件的函數(shù):
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
//也可以通過shortcutItem.localizedTitle判斷
switch shortcutItem.type {
case "0":
print("新消息")
break
case "1":
print("寫文章")
break
case "2":
print("搜索")
break
case "3":
print("分享")
break
default:
break
}
}
在這個函數(shù)中根據(jù)不同的選擇進行相應(yīng)的快速導(dǎo)航操作。
效果:
二、Peek & Pop
在添加這個功能前要首先檢測功能的可用性
self.traitCollection.forceTouchCapability == UIForceTouchCapability.available
下面以UITableView為例,很常規(guī)的創(chuàng)建一個UITableView。
-
注冊PreView功能
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")! cell.textLabel!.text = "indexPath:\((indexPath as NSIndexPath).row)" if self.traitCollection.forceTouchCapability == UIForceTouchCapability.available { self.registerForPreviewing(with: self, sourceView: cell) } return cell }
-
實現(xiàn)UIViewControllerPreviewingDelegate方法
//預(yù)覽 func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? { //在這個方法中可以通過previewingContext.sourceRect改變彈起視圖的Rect //location: 點擊的位置 let nextView = NextViewController() return nextView } //commit func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) { //使用show方法commit會有個彈性效果 self.showDetailViewController(viewControllerToCommit, sender: self) }
效果:
-
添加Peek Quick Actions功能
在要進入的ViewController中重寫:override var previewActionItems : [UIPreviewActionItem] { let action1 = UIPreviewAction(title: "收藏", style: UIPreviewActionStyle.default) { (action, viewController) in print("收藏") } let action2 = UIPreviewAction(title: "喜歡", style: UIPreviewActionStyle.default) { (action, viewController) in print("喜歡") } return [action1, action2] }
效果:
三、Force Properties
iOS9.0為UITouch增加了force等一系列屬性和方法。
獲取壓力值:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
print("按壓力度:(touch.force)")
}
}