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

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

CocoaHeadsSKG

February 24, 2016
Tweet

More Decks by CocoaHeadsSKG

Other Decks in Technology

Transcript

  1. 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
  2. What can it do on iOS? • Home Screen Quick

    Actions • Peek and Pop • Force Properties via UITouch 3
  3. 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
  4. Localising Home Screen Quick Actions • Add an InfoPlist.strings ◦

    Check your preferred languages ◦ Add “keys”=”values” for each language 7
  5. 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
  6. 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
  7. Accessing Quick actions in the application delegate App is running:

    func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) { completionHandler(handleShortcut(shortcutItem)) } 11
  8. 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
  9. 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
  10. Adding Peek & Pop for > 9.0 In your storyboard,

    in the segue attributes; just check Peek & Pop 15
  11. 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
  12. 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
  13. 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
  14. 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