iOS開發(fā)之 3D Touch

圖片來自網(wǎng)絡(luò)

前言

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)的鍵值對,如下:


01.jpeg

屬性:

  • 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)航操作。
效果:


02.jpeg

二、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)
    }
    

效果:


03.jpeg

04.jpeg
  • 添加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]
    }
    

效果:


05.jpeg

三、Force Properties

iOS9.0為UITouch增加了force等一系列屬性和方法。
獲取壓力值:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
print("按壓力度:(touch.force)")
}
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容