iOS 9: An Introduction to 3D Touch

Introduction

With the iPhone 6s and 6s Plus, Apple introduced an entirely new way of interacting with our devices called 3D Touch. 3D Touch works by detecting the amount of pressure that you are applying to your phone's screen in order to perform different actions. In this tutorial, I am going to show you how to take advantage of 3D Touch so that you can utilize this new technology in your own iOS 9 apps.

Prerequisites

This tutorial requires that you are running Xcode 7.1 or later. At the time of writing, the iOS Simulator doesn't support 3D Touch yet, which means that any testing needs to be done on a physical device, iPhone 6s or iPhone 6s Plus. If you'd like to follow along, then start by downloading the starter project from GitHub.

1. Peek and Pop in Storyboards

In this first section, I am going to show you how to implement Peek and Pop functionality in your app using storyboards—and a bit of code. If you don't know what Peek and Pop is, it's basically a way of pressing on a user interface element with a bit more force to get a "Peek" at it.

From such a preview, you can then either lift your finger to dismiss it or push a bit harder again to "Pop" it into full screen. "Peekable" items can be any view controller, including things such as emails, messages, and web pages as shown in the screenshot below.

Peeking at a web page

Open the starter project in Xcode and navigate to Main.storyboard. Zoom the storyboard out by pinching on your trackpad or by pressing Command + -. Select the segue shown in the next screenshot.

Selected Segue

With this segue selected, open the Attributes Inspector and look for a new section named Peek and Pop. Enable the checkbox and configure the behavior as shown below.

Peek and Pop options

In this menu, you can assign custom identifiers to both the Peek (Preview) and Pop (Commit) segues. The commit segue also has options to configure everything that you can for a regular segue in storyboards such as ClassModule, and Kind.

Build and run your app on your iPhone and press the + button in the top right corner to create a new item.

New item created

Press firmly on the item and you will see that we get a preview of the detail view controller for that item.

Item preview

You will see that our detail view does not yet show the right data for the item we are previewing. This is because we have not yet configured the view for the custom preview segue we defined in the storyboard. Back in your project, open MasterViewController.swift and replace the prepareForSegue(_:sender:) method with the following implementation:

The first if statement remains unchanged. If this code isn't executed, we then check to see if the sender is a UITableViewCell and the segue identifier is equal to "showDetailPeek". If both of these conditions are met, we configure the detailItem to the text of the cell.

Build and run your app again. This time, when peeking an item, you should get a properly configured preview as shown below.

Correct item preview

One important thing to note is that these storyboard configurations for Peek and Pop will only work on devices running iOS 9.1 or later. To support devices running iOS 9.0, you will need to configure your Peek and Pop functionality in code as shown in the next section.

2. Peek and Pop in Code

While a bit more complicated than the storyboard setup, programmatically implementing Peek and Pop also allows you to add extra actions to your previews when the user swipes up. Take a look at the following screenshot to better understand what I mean.

Preview actions

Peek and pop is handled in code by the UIViewControllerPreviewingDelegate protocol. In your project, create a new iOS > Source > Swift File and name it MasterPreviewing.

Swift source file

Add the following code to MasterPreviewing.swift:

We make the MasterViewController class conform to the UIViewControllerPreviewingDelegate protocol.

In the previewingContext(_:viewControllerForLocation:) method, we instantiate a ForceViewController from the storyboard and return this object. The preferredContentSize property determines how large the Peek preview will appear on the screen. When a size of (0, 0) is used, the preview automatically makes itself as large as it can for the current screen.

In the previewingContext(_:commitViewController:) method, we complete the transition from the MasterViewController instance and Pop the ForceViewController we created at the Peek stage on the screen.

To use this new code, we also need to register specific views which we want to generate a preview for when pushed on firmly. To do this, open MasterViewController.swift and add the following code to viewDidLoad():

We first check to see if 3D Touch is available on the device (referred to as Force Touch by the API). If that is the case, we register the forceButton (the one at the bottom of the table) as an eligible view to Peek and Pop with.

Finally, to add actions to a preview, you need to define them in the preview's view controller class. Open ForceViewController.swift and add the following method to the class:

We create three actions to display with the ForceViewController preview. The first is a regular action and is the most common. When this action is selected the (currently empty) block of code you define when creating the action will be executed. The second is a destructive action that will function exactly the same as the first, but it will appear red on the preview screen. Lastly, we create an action group that collapses any number of other actions under a single button.

Build and run your app once again. This time, push firmly on the Force button to see a new ForceViewController preview as shown below.

Force preview

Swipe up on this preview to see the actions that we defined in the ForceViewController class.

Force preview actions

Finally, press the Group... action to open up the actions contained in that group.

Force preview actions from group

3. Detecting Force Through UITouch

On 3D Touch compatible devices, the UITouch class also gains some new functionality in the form of two new properties, force and maximumPossibleForce. These properties are very useful for any use case where you want a precise measurement of how much pressure is being applied to the screen.

Start by adding the following method to the ForceViewController class:

We detect whenever a touch on the screen moves, including towards and away from the screen. We retrieve the current force being applied from the first UITouch object in the set and display the values on the screen.

Build and run your app and press the Force button to open the ForceViewController. Push anywhere on the screen with varying amounts of pressure and you will see that the label on the screen updates accordingly to show the current applied force as well as the maximum force that can be applied.

Detected force

Note that these values are not associated with any physical unit and are independent of the user's 3D Touch sensitivity settings. A value of 1.0 represents the force applied on an average touch.

4. Home Screen Quick Actions

In addition to the new in-app functionality that 3D Touch offers, you can also add up to four shortcuts for specific functions of your application on your app icon. These quick actions can be accessed when a user presses deeply on your app's icon on the home screen as shown in the next screenshot.

Safari quick actions

There are two main types of quick actions you can create for your app, static and dynamic. Static quick actions are defined in your app's Info.plist and are available at all times for your application. Dynamic quick actions are created in your code and are added to the shared UIApplication object for your app.

For our app, we are going to create both a static and a dynamic quick action that will have the exact same implementation, adding a new item to the table view. It will show you how to utilize both action types in your own applications.

Quick actions are represented by the new UIApplicationShortcutItem class, which has the following properties:

  • localizedTitle the main title of the quick action (e.g. New Tab in the above screenshot)
  • localizedSubtitle an optional subtitle for the quick action, which is displayed below the main title
  • type a unique string identifier for you to use to determine which quick action was selected
  • icon an optional UIApplicationShortcutIcon object that can display a system provided icon or a custom image
  • userInfo an optional dictionary, which is useful for associating data with a quick action

Firstly, we are going to create the static quick action. Open the target's Info.plist file and add the following items exactly as shown in the screenshot below:

Shortcut item keys

Note that the UIApplicationShortcutItemIconType key can be swapped with the UIApplicationShortcutItemIconFile key with the value being the image file name you want to use. The UIApplicationShortcutItemUserInfo value we provided is also just a basic example dictionary to show you how you can setup your own custom data.

Next, we are going to create the dynamic action. Open MasterViewController.swift and add the following two lines of code in the viewDidLoad() method:

Just like that, you have created both a static and dynamic quick action for your application.

Lastly, we need to handle our app's logic for when these quick actions are actually selected from the home screen. This is handled by your app delegate's application(_:performActionForShortcutItem:completionHandler:) method. Open AppDelegate.swift and add the following method to the AppDelegate class:

We first check the type of the quick action and then access the MasterViewController object. On this object, we call the insertNewObject(_:) method to insert a new item into the table view. Note that this method is provided by the the iOS > Application > Master-Detail Application template and requires an AnyObject parameter. This parameter is not used in the actual method implementation, however, and can be any object. Finally, we call the completionHandler with a boolean value to tell the system whether or not the quick action was executed successfully.

Build and run your app one last time. Once it has loaded, go to your device's home screen and push firmly on the app icon. You will see that two quick actions are available for your app.

Application quick actions

Next, press on either one of these and your application should open with a new item added to the table view.

New item added through shortcut

Conclusion

You should now be comfortable with the 3D Touch APIs available in iOS 9, including Peek and Pop, detecting force via UITouch, and home screen quick actions. 3D Touch offers many new ways of interacting with your device and I highly encourage everyone to adopt it within their own applications.

As always, you can leave your comments and feedback below.

Tags:

Comments

Related Articles