Upgrade to Pro — share decks privately, control downloads, hide ads and more …

3D Touch APIs

C887ad592770a197f114d0a1d3e3a5a7?s=47 Jorge Coca
November 11, 2015

3D Touch APIs

Learning lunch project that demonstrates the capabilities of Apple's 3D Touch technology


Jorge Coca

November 11, 2015


  1. 3D Touch APIs by Jorge Coca

  2. What is 3D Touch? 3D touch adds a new layer

    of interaction with the device. The device is no longer a 2-dimensional device; now it becomes a machine able to detect different levels of pressure and react accordingly.
  3. What can we do? • Home screen quick actions •

    Peek and pop in UIKit • Web view peek and pop in UIKit • UITouch force properties
  4. Home screen actions By applying a normal long press, we

    will be able to delete the app. By applying a bit extra of pressure, we will reveal the Home Screen Menu for the app.
  5. Peek & Pop

  6. Custom actions

  7. Sensibility can be customized

  8. Let's see the APIs

  9. Home Screen quick actions (1/5) Info.plist

  10. Home screen quick actions (2/5) AppDelegate func application(application: UIApplication, performActionForShortcutItem

    shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) { // handle your quick action request here }
  11. Home screen quick actions (3/5) Launch an app by a

    quick action: 1. application:willFinishLaunchingWithOptions: 2. application:didFinishLaunchingWithOptions: 3. application:performActionForShortcutItem:block
  12. Home screen quick actions (4/5) Resume an app by a

    quick action: Only application:performActionForShortcutItem:block is called
  13. Home screen quick actions (5/5) Create dynamic actions func createDynamicQuickActions()

    { let item = UIApplicationShortcutItem(type: "OpenDynamic", localizedTitle: "Dynamic Item") let items = [ item ] UIApplication.sharedApplication().shortcutItems = items }
  14. Peek & Pop Conform to UIViewControllerPreviewingDelegate

  15. UIViewControllerPreviewingDelegate • previewingContext:viewControllerForLocation: • Called when the user has pressed

    a source view in a previewing view controller, thereby obtaining a surrounding blur to indicate that a preview (peek) is available. • previewingContext:commitViewController: • Called to let you prepare the presentation of a commit (pop) view from your commit view controller.
  16. Register for 3D Touch events var forceTouchIsEnabled: Bool { get

    { return self.traitCollection.forceTouchCapability == .Available } } override func viewDidLoad() { super.viewDidLoad() if (forceTouchIsEnabled) { registerForPreviewingWithDelegate(self, sourceView: peekPopButton) } }
  17. UIViewControllerPreviewingDelegate func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {

    if let presentedVC = presentedViewController { if presentedVC.isKindOfClass(DetailViewController) { return nil } } let storyboard = UIStoryboard(name: "Main", bundle: nil) let detailsVC = storyboard.instantiateViewControllerWithIdentifier("DetailsViewController") return detailsVC } func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController) { let storyboard = UIStoryboard(name: "Main", bundle: nil) let detailsVC = storyboard.instantiateViewControllerWithIdentifier("DetailsViewController") showViewController(detailsVC, sender: self) }
  18. Preview Actions We can add custom actions to peek &

    pop by implementing the method previewActionItems() -> [UIPreviewActionItem] in the presented view controller
  19. Preview Actions override func previewActionItems() -> [UIPreviewActionItem] { let previewAction1

    = UIPreviewAction(title: "Log message", style: .Default) { (_, _) -> Void in print("Preview action performed") } let previewAction2 = UIPreviewAction(title: "Super Log!", style: .Destructive) { (_, _) -> Void in print("This log can be dangerous!") } let previewActions = [ previewAction1, previewAction2 ] let group = UIPreviewActionGroup(title: "Actions", style: .Default, actions: previewActions) let groups = [ group ] return groups }
  20. Reference • Github Repo: Dano3DTouch • Apple: Adopting 3D Touch