Book Image

iOS Programming Cookbook

Book Image

iOS Programming Cookbook

Overview of this book

Do you want to understand all the facets of iOS programming and build complex iOS apps? Then you have come to the right place. This problem-solution guide will help you to eliminate expensive learning curves and focus on specific issues to make you proficient at tasks and the speed-up time involved. Beginning with some advanced UI components such as Stack Views and UICollectionView, you will gradually move on to building an interface efficiently. You will work through adding gesture recognizer and touch elements on table cells for custom actions. You will work with the Photos framework to access and manipulate photos. You will then prepare your app for multitasking and write responsive and highly efficient apps. Next, you will integrate maps and core location services while making your app more secure through various encryption methods. Finally, you will dive deep into the advanced techniques of implementing notifications while working with memory management and optimizing the performance of your apps. By the end of the book, you will master most of the latest iOS 10 frameworks.
Table of Contents (16 chapters)

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: UIEvent?) { 
       if let touch = touches.first { 
           if #available(iOS 9.0, *) { 
               if traitCollection.forceTouchCapability ==.Available { 
                   // 3D Touch is avaialble in this device...