Accelerometer and gyroscope, accelerometer Gyroscope
If an instance of the motion manager needs to be created in the program, an instance should provide the accelerator and gyroscope motion services to the whole program. because the device only has one accelerator and one gyroscope, the use of Singleton is more logical.
The framework used to create the motion manager is CoreMotion. framework.
Introduce the header file # import <CoreMotion/CoreMotion. h>
// Initialize the motion manager CMMotionManager * motionManager = [[CMMotionManager alloc] init]; // determines whether the device supports the accelerator and gyroscope if (motionManager. accelerometerAvailable & motionManager. gyroAvailable) {// set the time for the accelerator to send an update motionManager every 0.01 seconds. accelerometerUpdateInterval =. 01; // receives the gyroscope motionManager. gyroUpdateInterval =. 01; // start the accelerator count update and specify the execution block for each accelerator count update [motionManager resume: [NSOperationQueue currentQueue] withHandler: ^ (CMAccelerometerData * accelerometerData, NSError * error) {// code block}]; [motionManager startGyroUpdatesToQueue: [NSOperationQueue currentQueue] withHandler: ^ (CMGyroData * gyroData, NSError * error) {// code block}];} else {NSLog (@ "the device does not support gyroscope ");}
If you want to stop receiving updates to the accelerometer and gyroscope
[MotionManager stopAccelerometerUpdates];
[MotionManager stopGyroUpdates];