iOS开发之 3D Touch

iOS开发之 3D Touch_第1张图片
图片来自网络

前言

3D Touch为用户提供了全新维度上的交互,在支持3D Touch的设备上,用户可以改变手指的按压力度使设备做出不同的响应。

3D Touch的主要功能:
1、Home Screen Quick Actions
2、Peek & Pop
3、Force Properties

下面介绍如何将3D Touch应用到项目中。

一、Home Screen Quick Actions

这一功能是通过重按主屏幕的icon呼出一个菜单,快速定位到相应的功能,有静态集成和动态集成两种方法,也可以将两种方法混用。

静态集成

静态集成是在info.plist文件中添加相应的键值对,如下:


iOS开发之 3D Touch_第2张图片
01.jpeg

属性:

  • UIApplicationShortcutItemTitle 标签名,必须
  • UIApplicationShortcutItemType 标签类型,可用于判断用户点击的标签,必须
  • UIApplicationShortcutItemSubtitle 副标题,可选
  • UIApplicationShortcutItemIconType 使用系统图标,可选
  • UIApplicationShortcutItemIconFile 使用boundle或者image asset catalog中的图片,35*35点,可选

动态集成

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool方法中添加:

if #available(iOS 9.0, *) {
    //这里使用系统图标
    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
}

处理点击事件的函数:

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
    }
}

在这个函数中根据不同的选择进行相应的快速导航操作。
效果:


iOS开发之 3D Touch_第3张图片
02.jpeg

二、Peek & Pop

在添加这个功能前要首先检测功能的可用性

self.traitCollection.forceTouchCapability == UIForceTouchCapability.available

下面以UITableView为例,很常规的创建一个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
    }
    
  • 实现UIViewControllerPreviewingDelegate方法

    //预览
    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)
    }
    

效果:


iOS开发之 3D Touch_第4张图片
03.jpeg

iOS开发之 3D Touch_第5张图片
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]
    }
    

效果:


iOS开发之 3D Touch_第6张图片
05.jpeg

三、Force Properties

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

你可能感兴趣的:(iOS开发之 3D Touch)