Chapter 20. Core Motion

20.0. Introduction

iOS devices are usually equipped with accelerometer hardware. Some iOS devices might also include a gyroscope, such as the new iPhone and the new iPad. Before attempting to use either the accelerometer or the gyroscope in your iOS applications, you must check the availability of these sensors on the iOS device on which your app runs. Recipe 20.3 and Recipe 20.4 include techniques you can use to detect the availability of the accelerometer and gyroscope. With a gyroscope, iOS devices are able to detect motion in six axes.

Let’s go through a situation that will show you the value of the gyroscope. The accelerometer cannot detect the rotation of the device around its vertical axis if you are holding the device perfectly still in your hands, sitting in a computer chair, and rotating your chair in a clockwise or counterclockwise fashion. From the standpoint of the floor or the Earth, the device is rotating around the vertical axis, but it’s not rotating around its own y-axis which is the vertical center of the device. So, the accelerometer does not detect any motion.

However, the gyroscope included in some iOS devices allows us to detect such movements. This allows more fluid and flawless movement detection routines. This is typically useful in games, where the developers need to know not only whether the device is moving on the x-, y-, and z-axes (information they can get from the accelerometer), but also whether it is changing in relation to ...

Get iOS 8 Swift Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.