$30 off During Our Annual Pro Sale. View Details »

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. Supporting 3D Touch on
    iPhone
    CocoaheadsSKG Dimitri James Tsiflitzis
    1

    View Slide

  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

    View Slide

  3. What can it do on iOS?
    ● Home Screen Quick Actions
    ● Peek and Pop
    ● Force Properties via UITouch
    3

    View Slide

  4. Static Home Screen Quick Actions
    4

    View Slide

  5. Adding static actions
    5

    View Slide

  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

    View Slide

  7. Localising Home Screen Quick Actions
    ● Add an InfoPlist.strings
    ○ Check your preferred languages
    ○ Add “keys”=”values” for each language
    7

    View Slide

  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

    View Slide

  9. Dynamic Home Screen Quick Actions
    9

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  14. Peek & pop
    Peek Preview Pop
    14

    View Slide

  15. Adding Peek & Pop for > 9.0
    In your storyboard, in the segue attributes; just check Peek & Pop
    15

    View Slide

  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)previewingContext
    commitViewController:(UIViewController *)viewControllerToCommit;
    - (UIViewController*)previewingContext:(id)previewingContext
    viewControllerForLocation:(CGPoint)location
    16

    View Slide

  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

    View Slide

  18. Force Properties
    In your view controller override these methods and check the first UITouch object
    func touchesBegan(touches: Set, withEvent event: UIEvent?)
    func touchesMoved(touches: Set, 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

    View Slide

  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

    View Slide

  20. Ευχαριστούμε :)
    @sprimp
    20

    View Slide