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
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
}
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. 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. Testing in Simulator
❖ SBShortcutMenuSimulator
❖ https://github.com/DeskConnect/
SBShortcutMenuSimulator
❖ Adds 3D touch ability to simulator
15. Implementation Steps
❖ Check that 3D touch is available
❖ Register the presenting VC as a preview delegate
❖ Implement
previewingContext:viewControllerForLocation
❖ Implement previewingContext:commitViewController
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. 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. 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. 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. Resources
❖ Apple - Getting Started with 3D Touch
❖ Laurenz.io - Peek and Pop Tutorial
❖ the-nerd.be - Another Peek and Pop Tutorial
21. Outta Time
❖ Andrew Kozlik
❖ @codefortravel
❖ UniKey
❖ https://github.com/akozlik/Back-to-the-Future
❖ Code for Orlando