Pressure Sensitive Gestures with 3D Touch and iOS 9

Kevin Mullins

ios9-3dtouch With the release of iOS 9, Apple is enabling developers to add pressure sensitive gestures to their iOS apps running on iPhone 6s and iPhone 6s Plus devices. Using the new 3D Touch APIs, an iPhone app is now able to not only tell that the user is touching the device’s screen, but is also able to sense how much pressure the user is exerting and respond to different levels of pressure.

3D Touch enables new levels of interaction and brings new functionality to iOS apps in the following ways.

Quick Actions

Using 3D Touch and Quick Actions, the developer can add common, quick, and easy to access shortcuts to functions in an iOS app from the Home screen icon on the iOS device.

You can think of Quick Actions like the contextual menus that can be popped-up when a user right-clicks on an item in a desktop app. Use Quick Actions to provide shortcuts to the most common functions or features of the app.

Defining Static Quick Actions

If one or more of the Quick Actions required by an app are static and do not need to change, the developer can define them in the app’s Info.plist file.

UIApplicationShortcutItems

    
        UIApplicationShortcutItemIconType
        UIApplicationShortcutIconTypeSearch
        UIApplicationShortcutItemSubtitle
        Will search for an item
        UIApplicationShortcutItemTitle
        Search
        UIApplicationShortcutItemType
        com.company.appname.000
    
    
        UIApplicationShortcutItemIconType
        UIApplicationShortcutIconTypeShare
        UIApplicationShortcutItemSubtitle
        Will share an item
        UIApplicationShortcutItemTitle
        Share
        UIApplicationShortcutItemType
        com.company.appname.001
    

Creating Dynamic Quick Action Items

In addition to defining static Quick Action items, the developer can create dynamic on-the-fly Quick Actions to the FinishedLaunching method of the AppDelegate.cs file in our Quick Action sample.

public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
{
    var shouldPerformAdditionalDelegateHandling = true;

    // Get possible shortcut item
    if (launchOptions != null) {
        LaunchedShortcutItem = launchOptions [UIApplication.LaunchOptionsShortcutItemKey] as UIApplicationShortcutItem;
        shouldPerformAdditionalDelegateHandling = (LaunchedShortcutItem == null);
    }
    
    // Add dynamic shortcut items
    if (application.ShortcutItems.Length == 0) {
        var shortcut3 = new UIMutableApplicationShortcutItem (ShortcutIdentifier.Third, "Play") {
            LocalizedSubtitle = "Will play an item",
            Icon = UIApplicationShortcutIcon.FromType(UIApplicationShortcutIconType.Play)
        };

        var shortcut4 = new UIMutableApplicationShortcutItem (ShortcutIdentifier.Fourth, "Pause") {
            LocalizedSubtitle = "Will pause an item",
            Icon = UIApplicationShortcutIcon.FromType(UIApplicationShortcutIconType.Pause)
        };

        // Update the application providing the initial 'dynamic' shortcut items.
        application.ShortcutItems = new UIApplicationShortcutItem[]{shortcut3, shortcut4};
    }

    return shouldPerformAdditionalDelegateHandling;
}

Peek and Pop

3D Touch provides new ways for a user to interact with information within an app quicker than ever, without having to navigate from their current location.

For example, if an app is displaying a table of messages, the user can press hard on on an item to preview its content in an overlay view (which Apple refers to as a Peek). If the user presses harder, they will enter the regular message view (which is referred to as Pop-ping into the view). The following code is from our peek and pop sample:

public MasterViewController MasterController { get; set; }
...

/// Present the view controller for the "Pop" action.
public override void CommitViewController (IUIViewControllerPreviewing previewingContext, UIViewController viewControllerToCommit)
{
  // Reuse Peek view controller for details presentation
  MasterController.ShowViewController(viewControllerToCommit,this);
}

/// Create a previewing view controller to be shown at "Peek".
public override UIViewController GetViewControllerForPreview (IUIViewControllerPreviewing previewingContext, CGPoint location)
{
  // Grab the item to preview
  var indexPath = MasterController.TableView.IndexPathForRowAtPoint (location);
  var cell = MasterController.TableView.CellAt (indexPath);
  var item = MasterController.dataSource.Objects [indexPath.Row];

  // Grab a controller and set it to the default sizes
  var detailViewController = MasterController.Storyboard.InstantiateViewController ("DetailViewController") as DetailViewController;
  detailViewController.PreferredContentSize = new CGSize (0, 0);

  // Set the data for the display
  detailViewController.SetDetailItem (item);
  detailViewController.NavigationItem.LeftBarButtonItem = MasterController.SplitViewController.DisplayModeButtonItem;
  detailViewController.NavigationItem.LeftItemsSupplementBackButton = true;

  // Set the source rect to the cell frame, so everything else is blurred.
  previewingContext.SourceRect = cell.Frame;

  return detailViewController;
}

Pressure Sensitivity

By using the new properties of the UITouch class an app can measure the amount of pressure the user is applying to the iOS device’s screen and use this information in its user interface. For example, making a brush stroke more translucent or opaque based on the amount of pressure.

As a result of 3D Touch, if a Xamarin.iOS app is running on iOS 9 (or greater) and the iOS device is capable of supporting 3D Touch, changes in pressure will cause the TouchesMoved event to be raised. When monitoring the TouchesMoved event of a UIView, an app can get the current pressure that the user is applying to the screen.

public override void TouchesMoved (NSSet touches, UIEvent evt)
{
    base.TouchesMoved (touches, evt);
    UITouch touch = touches.AnyObject as UITouch;
    if (touch != null)
    {
        // Get the pressure
        var force = touch.Force;
        var maxForce = touch.MaximumPossibleForce;
        
        // Do something with the touch and the pressure
        ...
    }
}

Wrapping Up

3D Touch enables new functionality and convenience features in a Xamarin.iOS app, from Quick Actions to access an app’s most common functions or features to quick and easy access to information details using Peek and Pop, and finally Pressure Sensitivity for drawing or game apps.

You can learn more about 3D Touch and download sample projects from the iOS 9 Samples Gallery today.

0 comments

Discussion is closed.

Feedback usabilla icon