3D Touch in iOS 9, Part 2: Preview Actions

Photo Sep 26, 10 57 04

If you’ve implemented a Peek (or preview) in your app as described in my previous article, you may be interested in implementing Preview Actions as well. Those come up when the user triggers the Peek preview and swipes up. Notice the arrow icon at the top of the preview if such actions are available.

It’s easy to add them to your preview controller, although it’s not as clearly described in the Apple documentation as it perhaps should be. Here’s how to do it.

In a nutshell, we need to create an array of UIPreviewActions, each of which has a title, a style and a completion block. Then we replace this array in our preview view controller (it works much like overriding a method with our own). The result looks just like an UIActionSheet.

Those methods look a little scary because the parameters are so long, but code completion will gladly help you out.

Each Preview Action can have a style of default, destructive (red) or selected (with a tick mark). There’s no need to implement a delegate for reacting to the outcome of those actions, because each action has a completion block attached to it, in which you can define what shall happen if the user selects the action.

Preview Actions can in fact be grouped together to create submenus. The principle is very similar to the above:

  • create your actions and add them to an array
  • add the array to a UIPreviewActionGroup
  • repeat for other groups as necessary
  • add all groups to yet another array
  • return the array of action groups in previewActionItems

Here’s an example (with only a single group, based on the above):

There’s no need to deal with dismissals or transitions, iOS will handle this beautifully for us.

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.