Slide 1

Slide 1 text

3D Touch APIs by Jorge Coca

Slide 2

Slide 2 text

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.

Slide 3

Slide 3 text

What can we do? • Home screen quick actions • Peek and pop in UIKit • Web view peek and pop in UIKit • UITouch force properties

Slide 4

Slide 4 text

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.

Slide 5

Slide 5 text

Peek & Pop

Slide 6

Slide 6 text

Custom actions

Slide 7

Slide 7 text

Sensibility can be customized

Slide 8

Slide 8 text

Let's see the APIs

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Home screen quick actions (2/5) AppDelegate func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) { // handle your quick action request here }

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Home screen quick actions (4/5) Resume an app by a quick action: Only application:performActionForShortcutItem:block is called

Slide 13

Slide 13 text

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 }

Slide 14

Slide 14 text

Peek & Pop Conform to UIViewControllerPreviewingDelegate

Slide 15

Slide 15 text

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.

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

Preview Actions We can add custom actions to peek & pop by implementing the method previewActionItems() -> [UIPreviewActionItem] in the presented view controller

Slide 19

Slide 19 text

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 }

Slide 20

Slide 20 text

Reference • Github Repo: Dano3DTouch • Apple: Adopting 3D Touch