Tag Archives: 3D Touch

How to display Live Photos in a view controller

Live Photo

Live Photos are a brand new kettle of fish: they’re not a UIImage (nor an NSImage), and they’re not a video resource either. To make use of them, Apple gave us a few new classes in iOS 9: PHLivePhoto and PHLivePhotoView. In addition there’s the the PHLivePhotoViewDelegate protocol we can utilise.

Let’s see how to grab a Live Photo from the user’s camera roll, check if it is in fact a Live Photo, and then display it in a Live Photo View. I’ll also describe how to animate the image programmatically.

Continue reading

How to track finger position and touch force in iOS 9

Female holds and touch tablet computer isolated on white background

In iOS 9.1 the UITouch Class has been amended to track several new options:

  • the force property will track how hard the user presses onto the screen
  • the preciseLocationInView property will track positions more accurately
  • altitude and azimuth detection for Apple Pencil

Here’s how to track all these options in our view.

Continue reading

3D Touch in iOS 9, Part 4: Adding Static Shortcut Items


The array of shortcut items can also be added to our app’s Info.plist. The result is almost the same as generating this array in code (as discussed in my previous article): the difference is that static shortcuts are available as soon as the app is installed, even if the user hasn’t opened it yet. With dynamic shortcuts, the app needs to be opened at least once for the shortcuts to be populated.

To get started, select your app’s Info.plist file in Xcode. If you’ve never used the plist editor before, you’re in for a little culture shock (because it’s neither fun nor intuitive to use). Here’s what you do step by step.

Add a new row to the file by right-clicking at the bottom of the list. Call the new row UIApplicationShortcutItems and turn it into an array. Notice that little triangle at the beginning of the row. Click to open it. Now hover over your new row and click on the plus icon that appears. This will create a new item in our array (whose name you can’t change). Hover over Item 0 and turn it into a dictionary, then open it with the disclosure triangle that has popped up.

This item will define your first shortcut. Click the plus icon in Item 0 to add a new row for each key and enter the following (or as many as you need):

  • UIApplicationShortcutItemTitle
  • UIApplicationShortcutItemSubtitle
  • UIApplicationShortcutItemType
  • UIApplicationShortcutItemIconFile

Give each of these an appropriate value until you get something like this:

Screen Shot 2015-09-28 at 07.30.34

Rinse and repeat for as many static shortcuts as you need. You have several other keys at your disposal, read all about those in Apple’s Info Plist Documentation.

Mixing with Dynamic Shortcuts

You can use both dynamic and static shortcuts if you like. In my previous article about dynamic shortcuts we’ve already seen how these are created. In order not to overwrite existing static shortcuts though, we need to add our dynamic shortcuts to the existing array we’ve just created rather than wipe it out (as we did before).

We can do this by using the arrayByAddingObjectsFromArray method, combining both arrays:

We react to static shortcuts just like we did to dynamic ones. I’ve explained how to do it in my previous article.

Demo Project

I’ve added all this static goodness to my demo project on GitHub:

3D Touch in iOS 9, Part 3: Adding Dynamic Shortcut Items


We can define shortcut items that appear when the user presses hard on the app icon from the home screen. If available, the springboard blurs and only leaves our app icon and the shortcut actions visible.

These items can be generated in two ways: via code, and via entries in our app’s Info.plist file. In this article I’ll show you how to create dynamic shortcut items in code. We’ll talk about Static Shortcut Items in my next article.

The difference between this approach and the Info.plist entries is that a user needs to have launched our app at least once for our shortcuts to become available. This is also true for changes to these items after an app upgrade. Another difference is that dynamic items can be updated, while static ones can not (unless we deploy a new version to iTunes Connect).

Shortcut Items without Icons

Shortcut items can be generated without icons with relative ease, as instances of the UIApplicationShortcutItem class. Here’s how to do it in your AppDelegate:

Call this method in applicationDidFinishLaunchingWithOptions. We’ll talk about how to react to these items in a moment.

Shortcut Items with your own Icons

Alternatively we can add swanky icons to these actions, as well as subtitles if we like. The method for this is another init method for the same class as above, taking more parameters. Icons are simple PNG files in 35×35 pixels (or 2x/3x equivalents of those dimensions). Add them to your asset catalogue and instantiate them like this:

This content is for members only.

Reacting to Shortcut Items

  1. There are two things that can happen when a user selects one of our shiny new shortcut items:
    either the app launches for the first time
  2. or the app is still open in the background

To cover both eventualities, we need to add two things to our AppDelegate.

The first is a check in applicationDidLaucnhingWithOptions to tell us how the app needs to be presented here. Think of the camera app for example: if the user selects the “selfie” option, the front camera needs to be active. Or if he wants to take a normal picture, the back camera needs to be active. We don’t want them to press other options, otherwise they might as well launch our app without a shortcut.

To make it simple here, I’ll print a log message and the title of our shortcut:

This content is for members only.

Further Reading

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.

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:

This content is for members only.

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.

This content is for members only.

Further Reading