O'Reilly logo

iOS Programming Cookbook by Hossam Ghareeb

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using 3D touch

Since the launch of iPhone 6s and 6s plus, Apple has introduced a new way of user interaction with mobile apps. A new dimension of touch event has been added by introducing 3D touch. By detecting how hard or deeply the user presses on the screen, you can do a specific action in your app. In the example below, we will see how to get the force of touch and log display on screen.

How to do it...

  1. As usual, open Xcode and create a new project with Single View template named 3D Touch.
  2. Open storyboard, and add a UILabel and place it at the center of the screen.
  3. Link the label with an IBOutlet to ViewController.swift.
  4. Go to ViewController.swift and override the following method:
 override func touchesMoved(touches: Set<UITouch>, withEvent event: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required