3D Touch in iOS 9, Part 1: Peek and Pop

Screen Shot 2015-09-26 at 09.48.21

With the introduction of the iPhone 6s we have some new ways of getting user input via 3D Touch. Now THAT’S innovative!

There are several things we can do with 3D Touch, and the first one we’ll explore here is Peek and Pop. When the user presses down a little harder, a Peek (or preview) appears, and if the user presses even harder, a Pop (or commit) view controller appears. Both are plain view controllers that are called via a delegate method we need to implement. Let’s see how this works.

Before we begin

The first thing we’ll need to do is to indicate that our view controller will conform to a new protocol. It’s called the UIViewControllerPreviewDelegate Protocol.

For backward compatibility I’ll also add a long press gesture recogniser here. Should our sample app be run on a device without 3D Touch support, at least the preview can be brought up via a long press gesture.

We’ll check if 3D Touch is available using something like the following method. To complete the setup, I’ve also included a custom initialiser for the long press gesture.

The above checks a new property of the UITraitCollection class called forceTouchCapability. If it returns the enum “available”, then the device is 3D Touch capable and it’s switched on. Note that at the time of writing, only the iPhone 6s and 6s Plus support this feature, and of course the iPad Pro when it arrives in November. Neither the iPhone 6 or earlier, nor the Simulator support the 3D Touch feature.

Because there’s a chance that the user may disable 3D Touch at any point, we can call this method in viewWillAppear. This will make sure it gets called when the app launches and when our main view comes back into vision. In that case, we’ll simple enable the long press gesture and the app keeps working. If the user switched 3D Touch on again, we’ll disable that gesture so it won’t interfere with 3D Touch.

We also want to call this method in traitCollectionDidChange, which is called when the user switches the 3D Touch feature on or off (among other occasions). Here’s how we do that:

[emember_protected]

With this prep work out of the way, let’s implement the view controllers.

Implementing Peek

iOS will call the previewingContext:viewControllerForLocation method when the user presses shallow. This is one of the two methods we need to conform to since we’ve volunteered for it with the above protocol.

All we have to do here is to return a dedicated view controller. I’m instantiating one from my storyboard here. The transition and background blur is automatically handled by iOS (thanks, team!)

I’ve noticed that iOS tries to do this multiple times and may throw an error message if our preview view is already being presented. To suppress this message I’m checking if a preview view is already being presented before presenting another one. Anything to make iOS happy!

You can get quite fancy with this method as you are passed the location in the view at which the user presses the screen. That’s how the Mail app figures out which cell to focus on.

[/emember_protected]

Make sure to implement a method that can dismiss the preview view controller, otherwise it’ll just sit on top of the stack and not go away again.

When the user swiped up while the preview view is displayed, we can display what’s known as Preview Action items. I’ll discuss this in my next article.

Implementing Pop

Once the preview is presented, the user can elect to press deeper to switch to another view controller. Again, all we have to do is to conform to the second method of our above protocol. This one is called previewingContext:commitViewController:viewControllerToCommit.

Just like before, I’ll instantiate a view controller from my storyboard here.

[emember_protected]

And again, make sure your commit view controller has some navigational mechanism to be dismissed. You can dismiss either the preview or the commit view controller with something like this:

And that’s it in a nutshell!

There’s a lot we can do with 3D Touch, and I’ll cover more in the few next articles. Check out my demo project below for all those funky implementations, and don’t forget that 3D Touch can currently only be tested on real devices.

Demo Project

[/emember_protected]

Further Reading

About Jay Versluis

Jay is a medical miracle known as Super Survivor. He runs two YouTube channels, five websites and several podcast feeds. To see what else he's up to, and to support him on his mission to make the world a better place, check out his Patreon Campaign.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.