How To Add 3D Touch To Your IOS App

  • By admin
  • October 16, 2019
  • 0


3D Contact Utility
The 3D Contact expertise was first launched on the iPhone 6s and 6s +. Units supporting 3D Contact are geared up with a faucet pressure delicate show, measuring the stress on the display. The 3D Contact expertise permits customers to press an app icon on the Dwelling display and get a fast entry to some performance introduced within the app. Additionally, inside an app, a person can get entry to some options.

From iOS 9, Apple made 3D Contact APIs obtainable:

  • Dwelling Display screen Fast Motion API
  • UIKit peek and pop API
  • Net view peek and pop API
  • UITouch pressure properties

As a way to discover out whether or not a tool helps the 3D Contact expertise, it’s a must to learn out the forceTouchCapability values. Whereas the app is working, a person can flip off 3D Contact, so this worth must be checked within the traitCollectionDidChange delegate methodology.

  - (void) traitCollectionDidChange: (UITraitCollection *) previousTraitCollection { 
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
NSLog (@ "3D Contact is obtainable");
} else {
NSLog (@ "3D Contact just isn't obtainable on this system");

3D Contact Fast Actions
There are two forms of Dwelling Display screen Fast Actions: dynamic and static.

Static actions are outlined within the Information.plist file throughout the UIApplicationShortcutItems array.

Dynamic actions need to be added to the UIApplication utility object within the shortcutItems property. You should utilize two strategies for creation:

Methodology 1

  init (sort: String, 
localizedTitle: String,
localizedSubtitle: String ?,
icon: UIApplicationShortcutIcon ?,
userInfo: [AnyHashable: Any]? = nil)

This methodology creates a Dwelling display dynamic fast motion with a header, elective subheader, elective icon, and elective person data dictionary.

Methodology 2

  comfort init (sort: String, 
localizedTitle: String)

Creates a Dwelling display dynamic fast motion with a header however with no icon.

Fast Actions Handler
func utility (utility: UIApplication,

performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: Bool -> Void) {

let didHandle: Bool = / * deal with the fast motion utilizing shortcutItem * /

completionHandler (didHandle)


func utility (utility: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

var performAdditionalHandling = true

if let shortcutItem = launchOptions? [UIApplicationLaunchOptionsShortcutItemKey]

as? UIApplicationShortcutItem {

/ * deal with the fast motion utilizing shortcutItem * /

performAdditionalHandling = false


return performAdditionalHandling


UIKit peek and pop API
This API is used for content material preview (fast) and additional transition to it. New strategies in UIViewController for ViewController registration and registration cancellation permit notifications as as to whether it will be utilized by 3D Contact. Moreover added are new protocols for 3D Contact help.

ViewController registration:
– (id) registerForPreviewingWithDelegate: (id) delegate sourceView: (UIView *) sourceView;


– (UIViewController *) previewingContext: (id) previewingContext viewControllerForLocation: (CGPoint) location {

// test if we're not already displaying a preview controller

if ([self.presentedViewController isKindOfClass: [PreviewViewController class]]) {

return nil;


// shallow press: return the preview controller right here (peek)

UIStoryboard * storyboard = [UIStoryboard storyboardWithName: @ "Main" bundle: nil];

UIViewController * previewController = [storyboard instantiateViewControllerWithIdentifier: @ "PreviewView"];

return previewController;



– (void) previewingContext: (id) previewingContext commitViewController: (UIViewController *) viewControllerToCommit {

// deep press: carry up the commit view controller (pop)

UIStoryboard * storyboard = [UIStoryboard storyboardWithName: @ "Main" bundle: nil];

UIViewController * commitController = [storyboard instantiateViewControllerWithIdentifier: @ "CommitView"];

[self showViewController: commitController sender: self];

// alternatively, use the view controller that's being offered right here (viewControllerToCommit)


In preview, it’s also possible to add UIPreviewAction and UIPreviewActionGroup

  UIPreviewAction * action1 = [UIPreviewAction actionWithTitle: @ "Motion 1" 
model: UIPreviewActionStyleDefault handler: ^ (UIPreviewAction * _Nonnull motion,
UIViewController * _Nonnull previewViewController) {
NSLog (@ "Motion 1 triggered");

  // add them to an arrary 
NSArray * actions = @ [action1, action2, action3];

  // add all actions to a gaggle 
UIPreviewActionGroup * group1 = [UIPreviewActionGroup actionGroupWithTitle: @ "Motion Group"
model: UIPreviewActionStyleDefault actions: actions];
NSArray * group = @ [group1];

The True Potential of 3D Contact
As builders are attending to know the advantages of the 3D expertise, it turns into clear that it’ll grow to be a staple.


Leave a Reply

Your email address will not be published. Required fields are marked *