CocoaheadsSKG #2 - Supporting 3D Touch on iPhone

CocoaheadsSKG #2 - Supporting 3D Touch on iPhone

@tsif gives a great talk on supporting 3D Touch on the iPhone

5a3cfb263fe4968b76c4c0bde721f047?s=128

CocoaHeadsSKG

February 24, 2016
Tweet

Transcript

  1. Supporting 3D Touch on iPhone CocoaheadsSKG Dimitri James Tsiflitzis 1

  2. What is it? • Force Touch is a technology developed

    by Apple Inc. in 2008, unveiled on September 9, 2014 during the Apple Watch reveal • Force Touch is a pressure sensitive multi-touch technology that enables trackpads and touchscreens to distinguish between different levels of force being applied to their surfaces • 3D Touch works by using capacitive sensors that measure microscopic changes in the distance between the backlight and the cover glass. 2
  3. What can it do on iOS? • Home Screen Quick

    Actions • Peek and Pop • Force Properties via UITouch 3
  4. Static Home Screen Quick Actions 4

  5. Adding static actions 5

  6. Adding static actions cont’d • UIApplicationShortcutItemSubtitle An optional string displayed

    below the title. • UIApplicationShortcutItemIconType Optional string for a system icon • UIApplicationShortcutItemUserInfo Optional dictionary with any additional information 6
  7. Localising Home Screen Quick Actions • Add an InfoPlist.strings ◦

    Check your preferred languages ◦ Add “keys”=”values” for each language 7
  8. Icons for Home Screen Quick Actions • Add icon templates

    for each shortcut to the asset file • These icons a single colour with sizes ◦ 35x35 @1 ◦ 70x70 @2 ◦ 105x105 @3 8
  9. Dynamic Home Screen Quick Actions 9

  10. Adding dynamic actions let shortcut = UIApplicationShortcutItem(type : "io.cocoaheadsskg.add-item", localizedTitle:

    "Add Item", localizedSubtitle: "addItemKey", icon: UIApplicationShortcutIcon(type: .Add), userInfo: nil) UIApplication.sharedApplication().shortcutItems = [shortcut] 10
  11. Accessing Quick actions in the application delegate App is running:

    func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) { completionHandler(handleShortcut(shortcutItem)) } 11
  12. Accessing Quick actions in the application delegate App was in

    the background func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool { if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey] as? UIApplicationShortcutItem { handleShortcut(shortcutItem) return false } return true } 12
  13. Accessing Quick actions in the application delegate private func handleShortcut(shortcutItem:

    UIApplicationShortcutItem) -> Bool { let shortcutType = shortcutItem.type guard let shortcutIdentifier = ShortcutIdentifier(fullIdentifier: shortcutType) else { return false } /* do your thing */ return yes } 13
  14. Peek & pop Peek Preview Pop 14

  15. Adding Peek & Pop for > 9.0 In your storyboard,

    in the segue attributes; just check Peek & Pop 15
  16. Adding Peek & Pop when you app supports legacy OS

    versions In viewDidLoad: if([self.traitCollection respondsToSelector:@selector(forceTouchCapability)] && (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)) { [self registerForPreviewingWithDelegate:self sourceView:self.tableView] } Preview delegate: - (void)previewingContext: (id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit; - (UIViewController*)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location 16
  17. Peek & Pop actions override func previewActionItems() -> [UIPreviewActionItem] {

    let regularAction = UIPreviewAction(title: "Regular", style: .Default) { (action: UIPreviewAction, vc: UIViewController) -> Void in } let destructiveAction = UIPreviewAction(title: "Destructive", style: .Destructive) { (action: UIPreviewAction, vc: UIViewController) -> Void in } let actionGroup = UIPreviewActionGroup(title: "Group...", style: .Default, actions: [regularAction, destructiveAction]) return [regularAction, destructiveAction, actionGroup] } Open your view controller and override the following method 17
  18. Force Properties In your view controller override these methods and

    check the first UITouch object func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { if let touch : UITouch? = touches.first { let maximumPossibleForce : CGFloat = touch.maximumPossibleForce let force : CGFloat = touch.force let forcePercentage : CGFloat = force / maximumPossibleForce } } If you support < 9.0 use if #available 18
  19. Simulator support From the horse’s mouth With Xcode 7.0 you

    must develop on a device that supports 3D Touch. Simulator in Xcode 7.0 does not support 3D Touch. In the wild https://github.com/Flipboard/FLEX https://github.com/DeskConnect/SBShortcutMenuSimulator ^ These use private API’s. Please remove them from your release builds 19
  20. Ευχαριστούμε :) @sprimp 20