Share
Embed
3D Touch APIs
Learning lunch project that demonstrates the capabilities of Apple's 3D Touch technology
Jorge Coca
November 11, 2015
Transcript
20 slides
-
What is 3D Touch? 3D touch adds a new layer
of interaction with the device. The device is no longer a 2-dimensional device; now it becomes a machine able to detect different levels of pressure and react accordingly. -
What can we do? • Home screen quick actions •
Peek and pop in UIKit • Web view peek and pop in UIKit • UITouch force properties -
Home screen actions By applying a normal long press, we
will be able to delete the app. By applying a bit extra of pressure, we will reveal the Home Screen Menu for the app. -
Home screen quick actions (2/5) AppDelegate func application(application: UIApplication, performActionForShortcutItem
shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) { // handle your quick action request here } -
Home screen quick actions (3/5) Launch an app by a
quick action: 1. application:willFinishLaunchingWithOptions: 2. application:didFinishLaunchingWithOptions: 3. application:performActionForShortcutItem:block -
Home screen quick actions (4/5) Resume an app by a
quick action: Only application:performActionForShortcutItem:block is called -
Home screen quick actions (5/5) Create dynamic actions func createDynamicQuickActions()
{ let item = UIApplicationShortcutItem(type: "OpenDynamic", localizedTitle: "Dynamic Item") let items = [ item ] UIApplication.sharedApplication().shortcutItems = items } -
UIViewControllerPreviewingDelegate • previewingContext:viewControllerForLocation: • Called when the user has pressed
a source view in a previewing view controller, thereby obtaining a surrounding blur to indicate that a preview (peek) is available. • previewingContext:commitViewController: • Called to let you prepare the presentation of a commit (pop) view from your commit view controller. -
Register for 3D Touch events var forceTouchIsEnabled: Bool { get
{ return self.traitCollection.forceTouchCapability == .Available } } override func viewDidLoad() { super.viewDidLoad() if (forceTouchIsEnabled) { registerForPreviewingWithDelegate(self, sourceView: peekPopButton) } } -
UIViewControllerPreviewingDelegate func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
if let presentedVC = presentedViewController { if presentedVC.isKindOfClass(DetailViewController) { return nil } } let storyboard = UIStoryboard(name: "Main", bundle: nil) let detailsVC = storyboard.instantiateViewControllerWithIdentifier("DetailsViewController") return detailsVC } func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController) { let storyboard = UIStoryboard(name: "Main", bundle: nil) let detailsVC = storyboard.instantiateViewControllerWithIdentifier("DetailsViewController") showViewController(detailsVC, sender: self) } -
Preview Actions We can add custom actions to peek &
pop by implementing the method previewActionItems() -> [UIPreviewActionItem] in the presented view controller -
Preview Actions override func previewActionItems() -> [UIPreviewActionItem] { let previewAction1
= UIPreviewAction(title: "Log message", style: .Default) { (_, _) -> Void in print("Preview action performed") } let previewAction2 = UIPreviewAction(title: "Super Log!", style: .Destructive) { (_, _) -> Void in print("This log can be dangerous!") } let previewActions = [ previewAction1, previewAction2 ] let group = UIPreviewActionGroup(title: "Actions", style: .Default, actions: previewActions) let groups = [ group ] return groups }