Introduction to Core Motion
Many applications, games in particular, love to use the accelerometer sensor in the iPhone to let the user precisely tilt their device as an interaction mechanism. In the last section we used UIDevice
to get notifications about general orientation changes of the device, but there is a way to get much more precise data: the Core Motion framework. In this section, we'll be taking a look at some of the sensory data available through the CMMotionManager
class.
Before we get started, let's reset our ViewController
class to get rid of our UIDevice
experiments, and start with our Core Motion experiments (you can create a new project if you don't want to get rid of the old code):
import UIKit import CoreMotion class ViewController: UIViewController { let motionManager = CMMotionManager() override func viewDidLoad() { super.viewDidLoad() } }
Remember to link the CoreMotion
framework before you import at the top of the view controller...