3D Touch

iphone 6s給人驚喜之一就是3D Touch,今天就來瞅瞅它.
UITouch有個(gè)force屬性,一般的壓力感應(yīng)值為1.0,可以設(shè)置他的maximumPossibleForce最大值,即從0到這個(gè)值的范圍.可以利用這個(gè)壓感值來畫出不同粗細(xì)的線條.

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

    CGContextSetLineWidth(cxt, force * strokeWidth)

效果如下:

Screen Shot 2015-10-26 at 8.45.34 PM.png

  • Peek Pop
    當(dāng)你點(diǎn)擊6s上的郵件列表中的其中一個(gè)郵件時(shí),其他郵件會(huì)變?yōu)榘胪该?以突出你現(xiàn)在點(diǎn)擊的郵件,而當(dāng)你繼續(xù)按壓時(shí),則會(huì)彈出這個(gè)郵件的內(nèi)容,這個(gè)即為Peek(下圖2),繼續(xù)按壓,郵件內(nèi)容會(huì)顯示為全屏模式,即為Pop(下圖4).
    Screen Shot 2015-10-26 at 8.51.43 PM.png

如果要實(shí)現(xiàn)Peek和Pop需要實(shí)現(xiàn)代理方法

 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的內(nèi)容.

  • Preview actions
    可以為Peek內(nèi)容增加上提操作:
  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]
  }

效果如下:


Screen Shot 2015-10-26 at 9.29.08 PM.png

<h3>Home screen quick actions</h3>

Screen Shot 2015-10-26 at 9.32.42 PM.png

有兩種添加方法:

  • 靜態(tài)添加
    直接在plist文件里添加.
    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)
  }
  • 動(dòng)態(tài)添加
  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 = []
    }

拓展:

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

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