1、访问原始的Motion数据
#import <UIKit/UIKit.h> #import <CoreMotion/CoreMotion.h> @interface ViewController : UIViewController @property (strong, nonatomic) IBOutlet UILabel *xAccLabel; @property (strong, nonatomic) IBOutlet UILabel *yAccLabel; @property (strong, nonatomic) IBOutlet UILabel *zAccLabel; @property (strong, nonatomic) IBOutlet UILabel *xGyroLabel; @property (strong, nonatomic) IBOutlet UILabel *yGyroLabel; @property (strong, nonatomic) IBOutlet UILabel *zGyroLabel; @property (strong, nonatomic) IBOutlet UILabel *xMagLabel; @property (strong, nonatomic) IBOutlet UILabel *yMagLabel; @property (strong, nonatomic) IBOutlet UILabel *zMagLabel; @property (nonatomic, strong) CMMotionManager * motionManager; - (void)startUpdates; - (void)stopUpdates;
- (CMMotionManager *)motionManager { if (_motionManager == nil) { _motionManager = [[CMMotionManager alloc] init]; } return _motionManager; } - (void)startUpdates { // Start accelerometer if available if ([self.motionManager isAccelerometerAvailable]) { [self.motionManager setAccelerometerUpdateInterval:1.0/2.0]; //Update twice per second [self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler: ^(CMAccelerometerData *data, NSError *error) { self.xAccLabel.text = [NSString stringWithFormat:@"%f", data.acceleration.x]; self.yAccLabel.text = [NSString stringWithFormat:@"%f", data.acceleration.y]; self.zAccLabel.text = [NSString stringWithFormat:@"%f", data.acceleration.z]; }]; } // Start gyroscope if available if ([self.motionManager isGyroAvailable]) { [self.motionManager setGyroUpdateInterval:1.0/2.0]; //Update twice per second [self.motionManager startGyroUpdatesToQueue:[NSOperationQueue mainQueue] withHandler: ^(CMGyroData *data, NSError *error) { self.xGyroLabel.text = [NSString stringWithFormat:@"%f", data.rotationRate.x]; self.yGyroLabel.text = [NSString stringWithFormat:@"%f", data.rotationRate.y]; self.zGyroLabel.text = [NSString stringWithFormat:@"%f", data.rotationRate.z]; }]; } // Start magnetometer if available if ([self.motionManager isMagnetometerAvailable]) { [self.motionManager setMagnetometerUpdateInterval:1.0/2.0]; //Update twice per second [self.motionManager startMagnetometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler: ^(CMMagnetometerData *data, NSError *error) { self.xMagLabel.text = [NSString stringWithFormat:@"%f", data.magneticField.x]; self.yMagLabel.text = [NSString stringWithFormat:@"%f", data.magneticField.y]; self.zMagLabel.text = [NSString stringWithFormat:@"%f", data.magneticField.z]; }]; } } -(void)stopUpdates { if ([self.motionManager isAccelerometerAvailable] && [self.motionManager isAccelerometerActive]) { [self.motionManager stopAccelerometerUpdates]; } if ([self.motionManager isGyroAvailable] && [self.motionManager isGyroActive]) { [self.motionManager stopGyroUpdates]; } if ([self.motionManager isMagnetometerAvailable] && [self.motionManager isMagnetometerActive]) { [self.motionManager stopMagnetometerUpdates]; } }
- (void)applicationWillResignActive:(UIApplication *)application { [self.viewController stopUpdates]; } - (void)applicationDidBecomeActive:(UIApplication *)application { [self.viewController startUpdates]; }
2、访问设备的Motion数据
#import <UIKit/UIKit.h> #import <CoreMotion/CoreMotion.h> @interface ViewController : UIViewController @property (strong, nonatomic) IBOutlet UILabel *rollLabel; @property (strong, nonatomic) IBOutlet UILabel *pitchLabel; @property (strong, nonatomic) IBOutlet UILabel *yawLabel; @property (strong, nonatomic) IBOutlet UILabel *xRotLabel; @property (strong, nonatomic) IBOutlet UILabel *yRotLabel; @property (strong, nonatomic) IBOutlet UILabel *zRotLabel; @property (strong, nonatomic) IBOutlet UILabel *xGravLabel; @property (strong, nonatomic) IBOutlet UILabel *yGravLabel; @property (strong, nonatomic) IBOutlet UILabel *zGravLabel; @property (strong, nonatomic) IBOutlet UILabel *xAccLabel; @property (strong, nonatomic) IBOutlet UILabel *yAccLabel; @property (strong, nonatomic) IBOutlet UILabel *zAccLabel; @property (strong, nonatomic) IBOutlet UILabel *xMagLabel; @property (strong, nonatomic) IBOutlet UILabel *yMagLabel; @property (strong, nonatomic) IBOutlet UILabel *zMagLabel; @property (nonatomic, strong) CMMotionManager *motionManager; - (void)startUpdates; - (void)stopUpdates; @end
- (CMMotionManager *)motionManager { // Lazy initialization if (_motionManager == nil) { _motionManager = [[CMMotionManager alloc] init]; } return _motionManager; } - (void)startUpdates { // Start device motion updates if ([self.motionManager isDeviceMotionAvailable]) { //Update twice per second [self.motionManager setDeviceMotionUpdateInterval:1.0/2.0]; [self.motionManager startDeviceMotionUpdatesUsingReferenceFrame: CMAttitudeReferenceFrameXMagneticNorthZVertical toQueue:[NSOperationQueue mainQueue] withHandler: ^(CMDeviceMotion *deviceMotion, NSError *error) { // Update attitude labels self.rollLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.attitude.roll]; self.pitchLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.attitude.pitch]; self.yawLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.attitude.yaw]; // Update rotation rate labels self.xRotLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.rotationRate.x]; self.yRotLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.rotationRate.y]; self.zRotLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.rotationRate.z]; // Update user acceleration labels self.xGravLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.gravity.x]; self.yGravLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.gravity.y]; self.zGravLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.gravity.z]; // Update user acceleration labels self.xAccLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.userAcceleration.x]; self.yAccLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.userAcceleration.y]; self.zAccLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.userAcceleration.z]; // Update magnetic field labels self.xMagLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.magneticField.field.x]; self.yMagLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.magneticField.field.y]; self.zMagLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.magneticField.field.z]; }]; } } -(void)stopUpdates { if ([self.motionManager isDeviceMotionAvailable] && [self.motionManager isDeviceMotionActive]) { [self.motionManager stopDeviceMotionUpdates]; } }
- (void)applicationWillResignActive:(UIApplication *)application { [self.viewController stopUpdates]; } - (void)applicationDidBecomeActive:(UIApplication *)application { [self.viewController startUpdates]; }