How to add 3D Touch to your IOS application

3D Touch Utility

3D Touch technology was first introduced on the iPhone 6s and 6s +. Devices compatible with 3D Touch are equipped with a touch-sensitive display, which measures the pressure on the screen. 3D Touch technology allows users to press an application icon on the home screen and get quick access to some functions presented in the application. Also, within an application, a user can access some functions.

Since iOS 9, Apple made the 3D Touch APIs available:

  • Home Screen Quick Action API

  • API UIKit peek and pop

  • Peek and pop web view API

  • UITouch force properties

To find out if a device supports 3D Touch technology, you must read the forceTouchCapability values. While the application is running, a user can disable 3D Touch, so this value must be checked in the traitCollectionDidChange delegate method.

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {

if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
NSLog(@"3D Touch is available");
} else {
NSLog(@"3D Touch is not available on this device");
}
}

Quick Actions 3D Touch

There are two types of quick actions on the home screen: dynamic and static.

Static actions are defined in the Info.plist file inside UIApplicationShortcutItems training.

Dynamic actions must be added to the UIA application application object in the shortcutItems property. You can use two methods for creation:

Method 1

init(type: String,

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

This method creates a Dynamic Quick Start Screen Action with a header, optional subtitle, optional icon, and optional User information dictionary.

Method 2

convenience init(type: String,

localizedTitle: String)

Create a Dynamic Quick Start Screen Action with a header but no icon.

Quick Actions Controller

func app (app: UIApplication,

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

let didHandle: Bool = / * handle quick action using shortcutItem * /

CompletionHandler (didHandle)

}

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

var performAdditionalHandling = true

if you leave shortcutItem = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey]

What? UIApplicationShortcutItem {

/ * handle quick action using shortcutItem * /

performAdditionalHandling = false

}

return performAdditionalHandling

}

API UIKit peek and pop

This API is used for (fast) content preview and further transition to it. New methods in UIViewController by ViewController registration and deregistration allow notifications as to whether 3D Touch will use it. Additionally, new protocols are added for 3D Touch support.

ViewController log:

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

Look:

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

// check if we are not showing a preview controller yet

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

return nil;

}

// shallow click: return the preview controller here (peek)

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

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

return previewController;

}

Commit:

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

// deep click: open commit view controller (popup)

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

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

[self showViewController:commitController sender:self];

// alternatively use view controller provided here (viewControllerToCommit)

}

In the preview, you can also add UIPreviewAction and UIPreviewActionGroup

UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"Action 1"

style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action,
UIViewController * _Nonnull previewViewController) {
NSLog(@"Action 1 triggered");
}];

// add them to an arrary

NSArray *actions = @[action1, action2, action3];

// add all actions to a group

UIPreviewActionGroup *group1 = [UIPreviewActionGroup actionGroupWithTitle:@"Action Group"
style:UIPreviewActionStyleDefault actions:actions];
NSArray *group = @[group1];

The true potential of 3D touch

As developers learn about the benefits of 3D technology, it is clear that it will become a staple.

Related Post

Leave a Reply

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