peek and pop

3D Touch: Peek And Pop

Besides quick actions, peek and pop is another very interesting 3D Touch feature. By using it you can quickly preview content, for example emails or websites.

Hint: This post is using Swift 3, Xcode 8.2.1 and iOS 10

Let’s take a look at an example, that isn’t using peek and pop yet:

A table with three rows will be displayed:

After tapping one of these cells, a new view controller will be pushed:

peek and pop

Note that we aren’t using segues in this example. There is a lot of discussion going on whether you should use storyboards or not. Either way, this doesn’t have an impact on the way how peek and pop has to be implemented.

Peek and Pop

There are three steps for implementing peek and pop:

  1. Register a view for previewing.
  2. Return the preview controller for peeking.
  3. Present the preview controller for popping.

To register a view for previewing, you just have to call one method. We do this in viewDidLoad:

In our example, the table view is the view, that should be available for peek and pop. In order for that method to be able to work, our view controller has to conform to the UITableViewControllerPreviewingDelegate protocol:

This protocol forces you to implement two methods. And these methods are those for steps 2 and 3.

First, we will implement the method, that returns the view controller for peeking:

If you are force touching a cell, the following appears:

Now, we implement the method that actually presents the view controller for popping:

Altogether, the view controllers’s source looks as follows:

That was not a lot of work, was it?

Conclusion

Peek and pop is the most important 3D touch action. Since it’s quickly implemented, it’s a good idea to ship this feature with your app.

Referenes

Title image: @ VOJTa Herout/ shutterstock.com

Book Tip

Big Nerd Range Guide: iOS Programming: Excellent introduction to iOS development. Some programming experience is recommended.

Amazon