Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Implementing 3D Touch
in iOS 9 - BTTF Style
Andrew Kozlik
Requirements
❖ iOS 9
❖ Xcode 7
❖ iPhone 6S or 6S Plus
❖ ALWAYS check to make sure force touch is available

func isForceTo...
Shortcut Example
Static Shortcuts
❖ Shortcuts are added to Info.plist
❖ Array - UIApplicationShortcutItems
❖ Each item contains keys includ...
Info.plist example
Dynamic Shortcuts
❖ Create an array of UIMutableApplicationShortcutItems
❖ Arguments Include:
❖ type
❖ localizedTitle
❖ lo...
Code Sample
func loadShortcutItems() {
var shortcutItems = [UIMutableApplicationShortcutItem]()
// Loop through each chara...
Responding to Shortcuts
❖ Application delegate implements
application:performActionForShortcutItem:
❖ Shortcut type identi...
performActionForShortcutItem
func application(application: UIApplication, performActionForShortcutItem shortcutItem:
UIApp...
Update UI Code Sample
func switchTimeTravelButtonLabel() {
var message = message1985;
if (buttonTimeTravel?.titleLabel?.te...
Deeplink Code Sample
func loadDetailForCharacter(characterName : String) {
let filteredCharacters = characters.filter() {
...
Shortcut Limitations
❖ Maximum of four menu items
❖ Your app must support deep linking for view controllers
for maximum va...
Testing in Simulator
❖ SBShortcutMenuSimulator
❖ https://github.com/DeskConnect/
SBShortcutMenuSimulator
❖ Adds 3D touch a...
Peek and Pop Functionality
Implementation Steps
❖ Check that 3D touch is available
❖ Register the presenting VC as a preview delegate
❖ Implement
pre...
previewingContext:viewControllerForLocation
❖ Register view with registerForPreviewingWithDelegate
❖ Pass in view to detec...
Code Sample
func previewingContext(previewingContext: UIViewControllerPreviewing,
viewControllerForLocation location: CGPo...
previewingContext:viewControllerToCommit
❖ Executed when user does second 3D touch action
❖ Handle any final logic
❖ Call s...
Parting Words of Advice
❖ Never use 3D touch as a primary interaction mechanism
❖ Use it to add optional functionality
❖ E...
Resources
❖ Apple - Getting Started with 3D Touch
❖ Laurenz.io - Peek and Pop Tutorial
❖ the-nerd.be - Another Peek and Po...
Outta Time
❖ Andrew Kozlik
❖ @codefortravel
❖ UniKey
❖ https://github.com/akozlik/Back-to-the-Future
❖ Code for Orlando
Credits
❖ Icon made by DKNG - http://modular4kc.com/
2014/07/11/17065/
Upcoming SlideShare
Loading in …5
×

3D Touch Implementation for Shortcuts and Peek/Pop Functionality

687 views

Published on

Andrew Kozlik gave this talk at the October 2015 Orlando iOS Meetup group. It gives a brief overview of 3D touch functionality and walks the reader through implementing shortcuts and peek/pop functionality.

The presentation code can be found on Github via https://github.com/akozlik/Back-to-the-Future

Published in: Mobile
  • Be the first to comment

  • Be the first to like this

3D Touch Implementation for Shortcuts and Peek/Pop Functionality

  1. 1. Implementing 3D Touch in iOS 9 - BTTF Style Andrew Kozlik
  2. 2. Requirements ❖ iOS 9 ❖ Xcode 7 ❖ iPhone 6S or 6S Plus ❖ ALWAYS check to make sure force touch is available
 func isForceTouchAvailable() -> Bool { var isForceTouchAvailable = false; if (self.traitCollection.respondsToSelector("forceTouchCapability")) { isForceTouchAvailable = self.traitCollection.forceTouchCapability == UIForceTouchCapability.Available } return isForceTouchAvailable }
  3. 3. Shortcut Example
  4. 4. Static Shortcuts ❖ Shortcuts are added to Info.plist ❖ Array - UIApplicationShortcutItems ❖ Each item contains keys including: ❖ UIApplicationShortcutItemIconFile ❖ UIApplicationShortcutItemTitle ❖ UIApplicationShortcutItemType ❖ UIApplicationShortcutItemUserInfo
  5. 5. Info.plist example
  6. 6. Dynamic Shortcuts ❖ Create an array of UIMutableApplicationShortcutItems ❖ Arguments Include: ❖ type ❖ localizedTitle ❖ localizedSubtitle ❖ icon ❖ userInfo ❖ Set UIApplication singleton shorcutItems property
  7. 7. Code Sample func loadShortcutItems() { var shortcutItems = [UIMutableApplicationShortcutItem]() // Loop through each character and create shortcuts for character in characters! { let item = UIMutableApplicationShortcutItem( type: "com.codefortravel.backtothefuture.character", localizedTitle: character.characterName!, localizedSubtitle: "Tap for more details", icon: nil, // No icon // icon: UIApplicationShortcutIcon(templateImageName: "flux- capacitor") // icon: UIApplicationShortcutIcon(type: .Add), userInfo: ["characterName" : character.characterName!] ) shortcutItems.append(item) } UIApplication.sharedApplication().shortcutItems = shortcutItems }
  8. 8. Responding to Shortcuts ❖ Application delegate implements application:performActionForShortcutItem: ❖ Shortcut type identifies different shortcuts ❖ Properties can be passed through userInfo ❖ Usage examples: ❖ Perform UI updates ❖ Handle deeplinking
  9. 9. performActionForShortcutItem func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) { if (shortcutItem.type == "com.codefortravel.backtothefuture.fluxcapacitor") { let VC = getRootController() as! ViewController VC.switchTimeTravelButtonLabel() } else if (shortcutItem.type == "com.codefortravel.backtothefuture.character") { let characterName = shortcutItem.userInfo!["characterName"] as? String let VC = getRootController() as! ViewController VC.loadDetailForCharacter(characterName!) } } func getRootController() -> UIViewController? { let navVC = self.window?.rootViewController as? UINavigationController let VCs = navVC?.viewControllers as [UIViewController]? if (VCs?.count > 0) { let VC = VCs![0] as! ViewController return VC } return nil }
  10. 10. Update UI Code Sample func switchTimeTravelButtonLabel() { var message = message1985; if (buttonTimeTravel?.titleLabel?.text == message1985) { message = message2015 } buttonTimeTravel?.setTitle(message, forState: UIControlState.Normal) }
  11. 11. Deeplink Code Sample func loadDetailForCharacter(characterName : String) { let filteredCharacters = characters.filter() { $0.characterName == characterName } if (filteredCharacters.count > 0) { let character = filteredCharacters[0] let detailVC = storyboard?.instantiateViewControllerWithIdentifier( "CharacterDetailViewController") as? CharacterDetailViewController detailVC?.character = character self.navigationController?.popToRootViewControllerAnimated(false) self.showViewController(detailVC!, sender: self) } }
  12. 12. Shortcut Limitations ❖ Maximum of four menu items ❖ Your app must support deep linking for view controllers for maximum value ❖ Shortcuts support up to two lines of text and one icon ❖ Icons must be square, single color, and 35x35 points ❖ User may turn 3D touch off, so monitor for changes to traitCollections ❖ Always test to ensure 3D touch is available
  13. 13. Testing in Simulator ❖ SBShortcutMenuSimulator ❖ https://github.com/DeskConnect/ SBShortcutMenuSimulator ❖ Adds 3D touch ability to simulator
  14. 14. Peek and Pop Functionality
  15. 15. Implementation Steps ❖ Check that 3D touch is available ❖ Register the presenting VC as a preview delegate ❖ Implement previewingContext:viewControllerForLocation ❖ Implement previewingContext:commitViewController
  16. 16. previewingContext:viewControllerForLocation ❖ Register view with registerForPreviewingWithDelegate ❖ Pass in view to detect force touch ❖ Method returns CGPoint indicating where in the view 3D touch was implemented ❖ For UITableViews and UICollectionViews, get the cell located at that point ❖ Handle any business logic ❖ Instantiate the preview view controller and return
  17. 17. Code Sample func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? { let indexPath = tableView?.indexPathForRowAtPoint(location) let row = indexPath?.row let character = characters[row!] let detailVC = storyboard?.instantiateViewControllerWithIdentifier("CharacterDetailViewCo ntroller") as? CharacterDetailViewController detailVC?.character = character detailVC?.preferredContentSize = CGSize(width: 0, height: 280) return detailVC }
  18. 18. previewingContext:viewControllerToCommit ❖ Executed when user does second 3D touch action ❖ Handle any final logic ❖ Call showViewController to display the VC in full screen func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController) { showViewController(viewControllerToCommit, sender: self) }
  19. 19. Parting Words of Advice ❖ Never use 3D touch as a primary interaction mechanism ❖ Use it to add optional functionality ❖ Educate your users that it exists ❖ Measure user engagement to determine best shortcuts ❖ Consider long press to replace 3D touch when unavailable
  20. 20. Resources ❖ Apple - Getting Started with 3D Touch ❖ Laurenz.io - Peek and Pop Tutorial ❖ the-nerd.be - Another Peek and Pop Tutorial
  21. 21. Outta Time ❖ Andrew Kozlik ❖ @codefortravel ❖ UniKey ❖ https://github.com/akozlik/Back-to-the-Future ❖ Code for Orlando
  22. 22. Credits ❖ Icon made by DKNG - http://modular4kc.com/ 2014/07/11/17065/

×