As we know that Apple has got a very different way to interact with the device in 6s and 6s plus by introducing 3D touch. In 3D touch, the phone judges how much pressure we are applying over the device and perform an appropriate action accordingly.

iPhone responds to the subtle tap when one interacts with the device and thus you will not only see what you are doing but you will feel it.

By 3D touch, user gets an additional interaction environment in iOS9.0 and above. Here the total pressure is calculated and appropriate action according to the pressure happens.

3D touch includes the following feature:

Peek and Pop Quick Action

Please note: You need a compatible device i.e. iPhone 6s and above, simulator does not support 3D touch.

Now let’s learn and implement the 3D feature Peek and Pop and will discuss Quick Action in next blog.

Peek and Pop:

in iOS development peek means previewing an item and performing an action without playing with your current context.

To check whether an item supports peek, press it lightly. If it displays a small rectangular box around called as hint, then sometimes it indicates that it supports peek.

A peek:

It will appear when user presses on an item that supports peek and disappears when an user removes his or her finger

Open a detailed view of the item – called pop – when the user presses a little deeper on peek view

It provides a quick action related to an item when the user scrolls-up on a peek view

Peek over an email

Object can be asked for the value of the forceTouchCapability property. If this capability is available we can register our view controller for peek and pop by calling registerForPreviewingWithDelegate:sourceView:



override func viewDidLoad() {

super.viewDidLoad()

if( traitCollection.forceTouchCapability == .Available){

registerForPreviewingWithDelegate(self, sourceView: self.collectionView!)}

}

Implementation of 3D touch:

We need to use delegate class for 3D touch i.e UIViewControllerPreviewingDelegate

The two required methods that we need to implement are:

The first will setup view controller for peeking,

The other one is for popping

Setup peeking:

This will help to show the content on the view that you want to show in preview, but not the complete content.



func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {

...

}

We only need to perform two more steps:

Set the preferredContentSize for the DetailViewController

detailVCObj.preferredContentSize = CGSize(width: 0.0, height: 300)]

Set the sourceRect . This property accepts a CGRect and will blur everything outside this rect, and keep the content inside the CGRect sharp.

previewingContext.sourceRect = cell.frame

Setup the popping:

To pop on the same view we just need to implement the delegate method on the same viewcontroller and call:

showViewController:sender: method.



func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController) {

showViewController(viewControllerToCommit, sender: self)

}



And now we are done. Just run the application and press the view on which you added the function. It should popup and if you press harder you’ll pop inside the DetailViewController.

Adding preview actions:

When a user is in peek view, he just needs to swipe up, and an action sheet will appear with some action that the user selected and performed.

To do this, you just need to implement the previewActionItems method in DetailViewController.

This method returns an array of objects that implement the UIPreviewActionItem protocol.

This UIPreviewAction has 3 arguments that we need to provide :

Title: the title of the button

Style: the style for the button

Handler: the action that we want to get executed when an user taps on the button.

For Example :



override func previewActionItems() -> [UIPreviewActionItem] {

let likeAction = UIPreviewAction(title: "Like", style: .Default) { (action, viewController) ->

Void in

print("Like it? ")

}

let deleteAction = UIPreviewAction(title: "Delete", style: .Destructive) { (action, viewController) -> Void in

print("Deleted this !! ")

}

return [likeAction, deleteAction]

}

CONCLUSION:

This 3D touch is a rapidly growing technology and every day some new development happens to make this better, and thus Apple came up with a completely new environment where we can use some 3D features, that help us in the following: