{"id":21549,"date":"2015-12-07T11:20:19","date_gmt":"2015-12-07T19:20:19","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=21549"},"modified":"2015-12-07T11:20:19","modified_gmt":"2015-12-07T19:20:19","slug":"pressure-sensitive-gestures-with-3d-touch-and-ios-9","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/pressure-sensitive-gestures-with-3d-touch-and-ios-9\/","title":{"rendered":"Pressure Sensitive Gestures with 3D Touch and iOS 9"},"content":{"rendered":"<p>\t\t\t\t<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/ios9-3dtouch.png\"><img decoding=\"async\" class=\"alignright size-medium wp-image-21638\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/ios9-3dtouch-247x300.png\" alt=\"ios9-3dtouch\" width=\"247\" height=\"300\" \/><\/a> With the release of iOS 9, Apple is\u00a0enabling developers to\u00a0add pressure sensitive gestures to their\u00a0iOS apps running on iPhone 6s and iPhone 6s Plus devices. Using the new\u00a03D Touch APIs, an iPhone app is now able to not only tell that the user is touching the device&#8217;s screen, but is also able to sense how much pressure the user is exerting and respond to different levels of pressure.<\/p>\n<p>3D Touch enables new levels of interaction and brings new functionality to iOS apps in the following ways.<\/p>\n<h2 id=\"toc_5\">Quick Actions<\/h2>\n<p>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.<\/p>\n<p>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.<\/p>\n<h3 id=\"toc_6\">Defining Static Quick Actions<\/h3>\n<p>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&#8217;s <code>Info.plist<\/code> file.<\/p>\n<pre>UIApplicationShortcutItems\n\n    \n        UIApplicationShortcutItemIconType\n        UIApplicationShortcutIconTypeSearch\n        UIApplicationShortcutItemSubtitle\n        Will search for an item\n        UIApplicationShortcutItemTitle\n        Search\n        UIApplicationShortcutItemType\n        com.company.appname.000\n    \n    \n        UIApplicationShortcutItemIconType\n        UIApplicationShortcutIconTypeShare\n        UIApplicationShortcutItemSubtitle\n        Will share an item\n        UIApplicationShortcutItemTitle\n        Share\n        UIApplicationShortcutItemType\n        com.company.appname.001\n    \n<\/pre>\n<h3 id=\"toc_9\">Creating Dynamic Quick Action Items<\/h3>\n<p>In addition to defining static Quick Action items, the developer can create dynamic on-the-fly Quick Actions to the <code>FinishedLaunching<\/code> method of the <code>AppDelegate.cs<\/code> file in our <a href=\"https:\/\/developer.xamarin.com\/samples\/monotouch\/ios9\/ApplicationShortcuts\/\">Quick Action sample<\/a>.<\/p>\n<pre class=\"EnlighterJSRAW\">public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)\n{\n    var shouldPerformAdditionalDelegateHandling = true;\n\n    \/\/ Get possible shortcut item\n    if (launchOptions != null) {\n        LaunchedShortcutItem = launchOptions [UIApplication.LaunchOptionsShortcutItemKey] as UIApplicationShortcutItem;\n        shouldPerformAdditionalDelegateHandling = (LaunchedShortcutItem == null);\n    }\n    \n    \/\/ Add dynamic shortcut items\n    if (application.ShortcutItems.Length == 0) {\n        var shortcut3 = new UIMutableApplicationShortcutItem (ShortcutIdentifier.Third, \"Play\") {\n            LocalizedSubtitle = \"Will play an item\",\n            Icon = UIApplicationShortcutIcon.FromType(UIApplicationShortcutIconType.Play)\n        };\n\n        var shortcut4 = new UIMutableApplicationShortcutItem (ShortcutIdentifier.Fourth, \"Pause\") {\n            LocalizedSubtitle = \"Will pause an item\",\n            Icon = UIApplicationShortcutIcon.FromType(UIApplicationShortcutIconType.Pause)\n        };\n\n        \/\/ Update the application providing the initial 'dynamic' shortcut items.\n        application.ShortcutItems = new UIApplicationShortcutItem[]{shortcut3, shortcut4};\n    }\n\n    return shouldPerformAdditionalDelegateHandling;\n}<\/pre>\n<h2 id=\"toc_1\">Peek and Pop<\/h2>\n<p>3D Touch provides new ways for a user to interact with information within an\u00a0app quicker than ever, without having to navigate from their current location.<\/p>\n<p>For example, if an\u00a0app 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 <em>Peek<\/em>).\u00a0If the user presses harder, they will enter the regular message view (which is referred to as <em>Pop<\/em>-ping into the view). The following code is from our <a href=\"http:\/\/developer.xamarin.com\/samples\/monotouch\/ios9\/ViewControllerPreview\/\">peek and pop sample<\/a>:<\/p>\n<pre class=\"EnlighterJSRAW\">public MasterViewController MasterController { get; set; }\n...\n\n\/\/\/ Present the view controller for the \"Pop\" action.\npublic override void CommitViewController (IUIViewControllerPreviewing previewingContext, UIViewController viewControllerToCommit)\n{\n  \/\/ Reuse Peek view controller for details presentation\n  MasterController.ShowViewController(viewControllerToCommit,this);\n}\n\n\/\/\/ Create a previewing view controller to be shown at \"Peek\".\npublic override UIViewController GetViewControllerForPreview (IUIViewControllerPreviewing previewingContext, CGPoint location)\n{\n  \/\/ Grab the item to preview\n  var indexPath = MasterController.TableView.IndexPathForRowAtPoint (location);\n  var cell = MasterController.TableView.CellAt (indexPath);\n  var item = MasterController.dataSource.Objects [indexPath.Row];\n\n  \/\/ Grab a controller and set it to the default sizes\n  var detailViewController = MasterController.Storyboard.InstantiateViewController (\"DetailViewController\") as DetailViewController;\n  detailViewController.PreferredContentSize = new CGSize (0, 0);\n\n  \/\/ Set the data for the display\n  detailViewController.SetDetailItem (item);\n  detailViewController.NavigationItem.LeftBarButtonItem = MasterController.SplitViewController.DisplayModeButtonItem;\n  detailViewController.NavigationItem.LeftItemsSupplementBackButton = true;\n\n  \/\/ Set the source rect to the cell frame, so everything else is blurred.\n  previewingContext.SourceRect = cell.Frame;\n\n  return detailViewController;\n}<\/pre>\n<h2 id=\"toc_2\">Pressure Sensitivity<\/h2>\n<p>By using the new properties of the <a href=\"http:\/\/developer.xamarin.com\/api\/type\/UIKit.UITouch\/\">UITouch<\/a> class an app can measure the amount of pressure the user is applying to the iOS device&#8217;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.<\/p>\n<p>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 <code>TouchesMoved<\/code> event to be raised. When monitoring the <code>TouchesMoved<\/code> event of a <a href=\"http:\/\/developer.xamarin.com\/api\/type\/UIKit.UIView\/\">UIView<\/a>, an app can get the current pressure that the user is applying to the screen.<\/p>\n<pre class=\"EnlighterJSRAW\">public override void TouchesMoved (NSSet touches, UIEvent evt)\n{\n    base.TouchesMoved (touches, evt);\n    UITouch touch = touches.AnyObject as UITouch;\n    if (touch != null)\n    {\n        \/\/ Get the pressure\n        var force = touch.Force;\n        var maxForce = touch.MaximumPossibleForce;\n        \n        \/\/ Do something with the touch and the pressure\n        ...\n    }\n}<\/pre>\n<h2>Wrapping Up<\/h2>\n<p>3D Touch enables new functionality and convenience features in\u00a0a Xamarin.iOS app, from Quick Actions to access an app&#8217;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.<\/p>\n<p>You can learn more about <a href=\"http:\/\/developer.xamarin.com\/guides\/ios\/platform_features\/introduction_to_ios9\/3dtouch\/\">3D Touch<\/a>\u00a0and download sample projects\u00a0from the <a href=\"http:\/\/developer.xamarin.com\/samples\/ios\/iOS9\/\">iOS 9 Samples Gallery<\/a> today.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With the release of iOS 9, Apple is\u00a0enabling developers to\u00a0add pressure sensitive gestures to their\u00a0iOS apps running on iPhone 6s and iPhone 6s Plus devices. Using the new\u00a03D Touch APIs, an iPhone app is now able to not only tell that the user is touching the device&#8217;s screen, but is also able to sense how [&hellip;]<\/p>\n","protected":false},"author":1935,"featured_media":21638,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[6,4],"class_list":["post-21549","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-ios","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>With the release of iOS 9, Apple is\u00a0enabling developers to\u00a0add pressure sensitive gestures to their\u00a0iOS apps running on iPhone 6s and iPhone 6s Plus devices. Using the new\u00a03D Touch APIs, an iPhone app is now able to not only tell that the user is touching the device&#8217;s screen, but is also able to sense how [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/21549","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/1935"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=21549"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/21549\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=21549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=21549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=21549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}