3D Touch



Apple has announced iPhone 6s and 6s plus in last year. At the same time, they have announced some cool features. One of them is 3D touch.


So what is 3D touch?


3D Touch is basically an input method. We can say next level of multi-touching, it can sense and differentiate a soft and a hard touch.
It works with pressure applied on screen. When you apply pressure on the screen, it will perform one action and if you apply little more pressure, it will perform another action. Basically, it detects the amount of pressure applied on screen and performs an action according to that.
Since this is a hardware specific feature, it is supported only in iPhone 6s and 6s plus so far.


Advantages of 3D Touch :


Home screen quick actions:
It can be used to show shortcut items from app icon at home screen. By pressing hard on home screen icon, you can see this quick action menu. By using this feature, you can launch particular screen within the app.


Peek (preview):
The user can see the content of another screen without having to navigate from current screen, just press deeper on view and it will show you a preview of the content.
Also, you can show actions from preview screen. You just have to slide up on a preview screen to see action items.


Pop (launch):
If you want to see details of content, you have to tap, check, and if you went to the wrong place, then tap back to come on the previous screen.
To avoid this, see a preview of the content and if you want to go in details just press hard on the preview screen, it will launch details screen.


Pressure sensitivity:
In addition to Home screen actions and peek and pop, Apple has also provided a way to get pressure data through force and maximumPossibleForce properties of UITouch class.
Using these properties, apps can measure how hard or light the user is touching the screen and  can take advantage of that information.


Implementation of 3D features:


Home screen quick actions:
Quick action can be static or dynamic. As the name suggest static actions are visible to the user when they install or update the app. Where dynamic actions are visible only after the first launch of the application. For dynamic actions, the app should be launched at least once.These dynamic actions can be changed according to the condition.
Static quick actions get displayed first and then, if there’s any space left, dynamic quick actions. The preference is always given to static items and then dynamic items.
Each quick action can have a title, subtitle, and icon.


Static quick actions :
We can show these items by adding UIApplicationShortcutItems array in plist with different properties like:


UIApplicationShortcutItemType : Required – String to identify quick action
UIApplicationShortcutItemTitle : Required – Title for the item
UIApplicationShortcutItemSubtitle : Optional – Subtitle for the item
UIApplicationShortcutItemIconType : Optional – System icon
UIApplicationShortcutItemIconFile :Optional -Image file name for icon
UIApplicationShortcutItemUserInfo : Optional -Dictionary with app info


Dynamic quick actions :
 Following code shows an example of dynamic quick action.


//Icon for shortcut item
UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithTemplateImageName:@”iconFileName”];
//Create shortcut item
UIMutableApplicationShortcutItem *item = [[UIMutableApplicationShortcutItem alloc] initWithType:@”com.test.item” localizedTitle:@“Title” localizedSubtitle:@“Subtitle” icon:icon userInfo:nil];
//set shortcut item
[UIApplication sharedApplication].shortcutItems = [NSArray arrayWithObject:item];


Peek and Pop Implementation :
To show peek and pop, you have to register view controller for this:
 // Register controller for 3D Touch
   if(viewController.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)
             // This method registers view controller for support 3D touch
            [viewController registerForPreviewingWithDelegate:self sourceView:sourceView];       
Following methods in registered view controller will be invoked for showing peek and pop on soft press and hard press respectively:


// soft press: return the preview controller here (peek)
– (UIViewController*)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
// hardpress: bring up the details view controller (pop)
– (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController*)viewControllerToCommit