1. Access the original motion data
#import<UIKit/UIKit.h>#import<CoreMotion/CoreMotion.h>@interfaceViewcontroller: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. Accessing the device's motion data
#import<UIKit/UIKit.h>#import<CoreMotion/CoreMotion.h>@interfaceViewcontroller: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:cmattitudereferenceframexmagne Ticnorthzvertical toqueue:[nsoperationqueue Mainqueue] Withhandler: ^ (Cmdevicemotion *devicemotion, Nserror *error) { //Update Attitude LabelsSelf.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 LabelsSelf.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 LabelsSelf.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 LabelsSelf.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 labelsSelf.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];}
Core Motion sensor RAW data