{"id":33344,"date":"2016-03-31T16:30:56","date_gmt":"2016-03-31T11:00:56","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=33344"},"modified":"2016-04-14T12:28:11","modified_gmt":"2016-04-14T06:58:11","slug":"playing-with-3d-touch-ios","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/playing-with-3d-touch-ios\/","title":{"rendered":"Playing with 3D Touch\u2014iOS"},"content":{"rendered":"<p>As we know that Apple has got a very different way to interact with the device in 6s and 6s plus by introducing 3D touch. In 3D touch, the phone judges how much pressure we are applying over the device and perform an appropriate action accordingly.<\/p>\n<p>iPhone responds to the subtle tap when one interacts with the device and thus you will not only see what you are doing but you will feel it.<\/p>\n<p>By 3D touch, user gets an additional interaction environment in iOS9.0 and above. Here the total pressure is calculated and appropriate action according to the pressure happens.<\/p>\n<p>3D touch includes the following feature:<\/p>\n<ol>\n<li>Peek and Pop<\/li>\n<li>Quick Action<\/li>\n<\/ol>\n<p><em>Please note: You need a compatible device i.e. iPhone 6s and above, simulator does not support 3D touch.<\/em><\/p>\n<p>Now let\u2019s learn and implement the 3D feature <strong>Peek and Pop<\/strong><strong>\u00a0<\/strong>and will discuss <strong>Quick Action <\/strong>in next blog.<\/p>\n<h5><strong>Peek and Pop:<\/strong><\/h5>\n<p>in <a title=\"ios application development services\" href=\"http:\/\/www.tothenew.com\/mobile-ios-application-development-services\">iOS development<\/a> peek means previewing an item and performing an action without playing with your current context.<\/p>\n<p>To check whether an item supports peek, press it lightly. If it displays a\u00a0small rectangular box around called as <em>hint,\u00a0<\/em>then sometimes it indicates that it supports peek.<\/p>\n<h5>A peek:<\/h5>\n<ul>\n<li>It will appear when user presses on an item that supports peek and disappears when an user removes his or her finger<\/li>\n<li>Open a detailed view of the item \u2013 <em>called pop \u2013<\/em> when the user presses a little deeper on peek view<\/li>\n<li>It provides a quick action related to an item when the user scrolls-up on a peek view<\/li>\n<\/ul>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-33345 aligncenter\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/03\/Untitled.png\" alt=\"Untitled\" width=\"394\" height=\"619\" \/><\/p>\n<p style=\"text-align: center;\">\u00a0Peek over an email<\/p>\n<p>&nbsp;<\/p>\n<h5>Object can be asked for the value of the\u00a0<em>forceTouchCapability<\/em> property. If this capability is available we can register our view controller for peek and pop by calling\u00a0<em><a href=\"https:\/\/developer.apple.com\/library\/ios\/documentation\/UIKit\/Reference\/UIViewController_Class\/#\/\/apple_ref\/occ\/instm\/UIViewController\/registerForPreviewingWithDelegate:sourceView:\">registerForPreviewingWithDelegate:sourceView:<\/a><\/em><br \/>\n<code><br \/>\noverride func viewDidLoad() {<br \/>\nsuper.viewDidLoad()<br \/>\nif( traitCollection.forceTouchCapability == .Available){<br \/>\nregisterForPreviewingWithDelegate(self, sourceView: self.collectionView!)}<br \/>\n}<\/code><\/h5>\n<h5><strong>Implementation of 3D touch:<\/strong><\/h5>\n<p>We need to use delegate class for 3D touch i.e UIViewControllerPreviewingDelegate<\/p>\n<p>The two required methods that we need to implement are:<\/p>\n<ul>\n<li>The first will setup view controller for peeking,<\/li>\n<li>\n<h5>The other one is for popping<\/h5>\n<\/li>\n<\/ul>\n<h5><strong>Setup peeking:<\/strong><\/h5>\n<p>This will help to show the content on the view that you want to show in preview, but not the complete content.<\/p>\n<p><code><br \/>\nfunc previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -&gt; UIViewController? {<br \/>\n...<br \/>\n}<\/code><\/p>\n<p>We only need to perform two more steps:<\/p>\n<ul>\n<li>Set the\u00a0<em>preferredContentSize<\/em> for the <strong>DetailViewController<\/strong><\/li>\n<\/ul>\n<p><code>detailVCObj.preferredContentSize = CGSize(width: 0.0, height: 300)]<\/code><\/p>\n<ul>\n<li>Set the <em>sourceRect .<\/em> This property accepts a CGRect and will blur everything outside this rect, and keep the content inside the CGRect sharp.<\/li>\n<\/ul>\n<p><code>previewingContext.sourceRect = cell.frame<\/code><\/p>\n<h5><strong>Setup the popping:<\/strong><\/h5>\n<p>To pop on the same view we just need to implement the delegate method on the same viewcontroller and call:<\/p>\n<h5><em>showViewController:sender: <\/em>method.<br \/>\n<code><br \/>\nfunc previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController) {<br \/>\nshowViewController(viewControllerToCommit, sender: self)<br \/>\n}<br \/>\n<\/code><br \/>\nAnd now we are done. Just run the application and press the view on which you added the function. It should popup and if you press harder you\u2019ll pop inside the DetailViewController.<\/h5>\n<h5><strong>Adding preview actions:<\/strong><\/h5>\n<p>When a user is in peek view, he just needs to swipe up, and an action sheet will appear with some action that the user selected and performed.<\/p>\n<p>To do this, you just need to implement the\u00a0<em>previewActionItems<\/em> method in DetailViewController.<\/p>\n<p>This method returns an array of objects that implement the\u00a0<em>UIPreviewActionItem<\/em>\u00a0protocol.<\/p>\n<p>This <em>UIPreviewAction <\/em>has 3 arguments that we need to provide :<\/p>\n<ul>\n<li>Title: the title of the button<\/li>\n<li>Style: the style for the button<\/li>\n<li>Handler: the action that we want to get executed when an user taps on the button.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h5><strong>For Example :<\/strong><br \/>\n<code><br \/>\noverride func previewActionItems() -&gt; [UIPreviewActionItem] {<br \/>\nlet likeAction = UIPreviewAction(title: \"Like\", style: .Default) { (action, viewController) -&gt;<br \/>\nVoid in<br \/>\nprint(\"Like it? \")<br \/>\n}<br \/>\nlet deleteAction = UIPreviewAction(title: \"Delete\", style: .Destructive) { (action, viewController) -&gt; Void in<br \/>\nprint(\"Deleted this !! \")<br \/>\n}<br \/>\nreturn [likeAction, deleteAction]<br \/>\n}<\/code><\/h5>\n<h4><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-33346 aligncenter\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/03\/IMG_3256.jpg\" alt=\"IMG_3256\" width=\"375\" height=\"667\" \/><\/h4>\n<h5><strong>CONCLUSION<\/strong>:<\/h5>\n<p>This 3D touch is a rapidly growing technology and every day some new development happens to make this better, and thus Apple came up with a completely new environment where we can use some 3D features, that help us in the following:<\/p>\n<ol>\n<li>It gives us quick access to content<\/li>\n<li>We can enter directly to various controllers where we want to perform some actions<\/li>\n<li>We can peek into various things directly in spite of going for its details<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>As we know that Apple has got a very different way to interact with the device in 6s and 6s plus by introducing 3D touch. In 3D touch, the phone judges how much pressure we are applying over the device and perform an appropriate action accordingly. iPhone responds to the subtle tap when one interacts [&hellip;]<\/p>\n","protected":false},"author":514,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[1400,1772,1],"tags":[3170,4848,3171,3172],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/33344"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/514"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=33344"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/33344\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=33344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=33344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=33344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}