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...
As usual, open Xcode and create a new project with Single View template named 3D Touch.
Open storyboard, and add a
UILabel
and place it at the center of the screen.Link the label with an
IBOutlet
toViewController.swift
.Go to
ViewController.swift
and override the following method:
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { if let touch = touches.first { if #available(iOS 9.0, *) { if traitCollection.forceTouchCapability ==.Available { // 3D Touch is avaialble in this device...