3D Touch

iphone 6s给人惊喜之一就是3D Touch,今天就来瞅瞅它.
UITouch有个force属性,一般的压力感应值为1.0,可以设置他的maximumPossibleForce最大值,即从0到这个值的范围.可以利用这个压感值来画出不同粗细的线条.

     if  traintCollection.forceTouchCapability == .Available {
                    addLineFromPoint(touch.previousLocationInView(self),
          toPoint: touch.locationInView(self), withForce: touch.force)
     }

    CGContextSetLineWidth(cxt, force * strokeWidth)

效果如下:

3D Touch_第1张图片
Screen Shot 2015-10-26 at 8.45.34 PM.png

  • Peek Pop
    当你点击6s上的邮件列表中的其中一个邮件时,其他邮件会变为半透明,以突出你现在点击的邮件,而当你继续按压时,则会弹出这个邮件的内容,这个即为Peek(下图2),继续按压,邮件内容会显示为全屏模式,即为Pop(下图4).
    3D Touch_第2张图片
    Screen Shot 2015-10-26 at 8.51.43 PM.png

如果要实现Peek和Pop需要实现代理方法

 if traitCollection.forceTouchCapability == .Available {
      registerForPreviewingWithDelegate(self, sourceView: view)
    }
extension ViewController: UIViewControllerPreviewingDelegate {
  func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
    guard let indexPath = tableView.indexPathForRowAtPoint(location),
      cell = tableView.cellForRowAtIndexPath(indexPath) as? DoodleCell
      else { return nil }
    
    let identifier = "DoodleDetailViewController"
    guard let detailVC = storyboard?.instantiateViewControllerWithIdentifier(identifier) as? DoodleDetailViewController
      else { return nil }
        detailVC.doodle = cell.doodle
    detailVC.doodlesViewController = self
    previewingContext.sourceRect = cell.frame
        // peek
    return detailVC
  }
  func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController) {
    //pop!
    showViewController(viewControllerToCommit, sender: self)
  }  
}

detailVC是Peek的内容.

  • Preview actions
    可以为Peek内容增加上提操作:
  override func previewActionItems() -> [UIPreviewActionItem] {
    let shareAction = UIPreviewAction(title: "Share", style: .Default) {
      (previewAction, viewController) in
      if let doodlesVC = self.doodlesViewController,
        activityViewController = self.activityViewController {
          doodlesVC.presentViewController(activityViewController, animated:true, completion: nil)
      }
    }
    let deleteAction = UIPreviewAction(title: "Delete",
      style: .Destructive) {
        (previewAction, viewController) in
        guard let doodle = self.doodle else { return }
        Doodle.deleteDoodle(doodle)
        
        if let doodlesViewController = self.doodlesViewController {
          doodlesViewController.tableView.reloadData()
        }
    }
    return [shareAction, deleteAction]
  }

效果如下:


3D Touch_第3张图片
Screen Shot 2015-10-26 at 9.29.08 PM.png

Home screen quick actions

3D Touch_第4张图片
Screen Shot 2015-10-26 at 9.32.42 PM.png

有两种添加方法:

  • 静态添加
    直接在plist文件里添加.
    3D Touch_第5张图片
    Screen Shot 2015-10-26 at 9.34.55 PM.png

    在AppDelegate文件里添加:
  func application(application: UIApplication,
    performActionForShortcutItem shortcutItem: UIApplicationShortcutItem,
    completionHandler: (Bool) -> Void) {
      handleShortcutItem(shortcutItem)
      completionHandler(true)
  }
  
  func handleShortcutItem(shortcutItem: UIApplicationShortcutItem) {
    switch shortcutItem.type {
    case "com.razeware.Doodles.new":
      presentNewDoodleViewController()
    default: break
    }
  }
  
  func presentNewDoodleViewController() {
    let identifier = "NewDoodleNavigationController"
    let doodleViewController = UIStoryboard.mainStoryboard
      .instantiateViewControllerWithIdentifier(identifier)
    
    window?.rootViewController?
      .presentViewController(doodleViewController, animated: true,
        completion: nil)
  }
  • 动态添加
  static func configureDynamicShortcuts() {
    if let mostRecentDoodle = Doodle.sortedDoodles.first {
      let shortcutType = "com.razeware.Doodles.share"
      let shortcutItem = UIApplicationShortcutItem(type: shortcutType, localizedTitle: "Share Latest Doodle",
        localizedSubtitle: mostRecentDoodle.name,
        icon: UIApplicationShortcutIcon(type: .Share),
        userInfo: nil)
      UIApplication.sharedApplication().shortcutItems = [ shortcutItem ]
    } else {
      UIApplication.sharedApplication().shortcutItems = []
    }

拓展:

  • Adopting 3D Touch on iPhone
  • 可以在模拟器上模拟quick action的库
  • 3D touch 的小例子

你可能感兴趣的:(3D Touch)