Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Recipe 14-6 mimics the Apple motion detection system while avoiding the need for the event consumer to be the first responder. It’s built on two key parameters: a sensitivity level that provides a threshold that must be met before a shake is acknowledged and a lockout time that limits how often a new shake can be generated.
This AccelerometerHelper class stores a triplet of acceleration values. Each value represents a force vector in 3D space. Each successive pair of that triplet can be analyzed to determine the angle between the two vectors. In this example, the angles between the first two items and the second two help determine when a shake happens. This code looks for a pair whose second angle exceeds the first angle. If the angular movement has increased enough between the two (that is, an acceleration of angular velocity, basically a “jerk”), a shake is detected.