introduction of the framework:
#import <CoreMotion/CoreMotion.h>
To define property initialization correlation:
#import "ViewController.h" #import <CoreMotion/CoreMotion.h> @interface Viewcontroller () @property (nonatomic, Strong) Cmmotionmanager *motionmanager; @property (nonatomic, strong) Nsoperationqueue *quene; @end @implementation viewcontroller-(void) viewdidload { [super viewdidload]; Initialize Cmmotionmanager Self.motionmanager = [[Cmmotionmanager alloc]init]; Initialize nsoperationqueue self.quene = [[Nsoperationqueue alloc]init]; Invoke accelerator [self configureaccelerometer]; Call the gyroscope [self Configuregrro];}
Daily Update Attention:Http://weibo.com/hanjunqiangSina Micro-blog
Use of accelerators:
/*//Each device shakes, the system notifies each device that it is in use and can make itself the first responder-(BOOL) canbecomefirstresponder{return YES;} -(void) Viewdidappear: (BOOL) animated{[self becomefirstresponder];} *///Accelerator Method-(void) configureaccelerometer{/** * 5.0 before using pull mode, then use the push mode *//Pull mode//Determine if the accelerator can use if ([_motionmanager isaccelerometeravailable]) { [_motionmanager setaccelerometerupdateinterval:1/40.0]; [_motionmanager startaccelerometerupdates]; }else{NSLog (@ "accelerator not available"); } *///push mode if ([_motionmanager isaccelerometeravailable]) {//Set accelerator frequency [_motionmanager s Etaccelerometerupdateinterval:1/40.0]; Start collecting data [_motionmanager startaccelerometerupdatestoqueue:_quene withhandler:^ (cmaccelerometerdata *accelerometerD ATA, Nserror *error) {if (Fabs (accelerometerdata.acceleration.x) > 2.0 | | fabs (accelerometerdat A.ACCELERATION.Y) > 2.0 | | Fabs (ACCELEROMETERDATA.ACCELERATION.Z) > 2.0) {NSLog (@ "Vibration detected"); } NSLog (@ "%.2f__%.2f__%.2f", Accelerometerdata.acceleration.x,accelerometerdata.acceleration.y,accelerometerdata . acceleration.z); }]; }else{NSLog (@ "accelerator not available"); }//Touch End-(void) touchesended: (Nsset *) touches withevent: (uievent *) event{cmacceleration acceleration = _mo TionManager.accelerometerData.acceleration; NSLog (@ "%.2f__%.2f__%.2f", acceleration.x,acceleration.y,acceleration.z);}
Use of gyroscopes:
Use of gyroscopes-(void) configuregrro{ if ([_motionmanager isgyroavailable]) { [Self.motionmanager Startgyroupdatestoqueue:_quene withhandler:^ (Cmgyrodata *gyrodata, Nserror *error) { NSLog (@ "%.2f__%.2f__%.2f", GYRODATA.ROTATIONRATE.X,GYRODATA.ROTATIONRATE.Y,GYRODATA.ROTATIONRATE.Z);} ]; } else{ NSLog (@ "Gyro cannot be used");} }
Some of the ways that shaking triggers:
-(void) Viewdiddisappear: (BOOL) animated{ [Self.motionmanager stopaccelerometerupdates]; [Self.motionmanager stopgyroupdates];} Trigger when starting shaking-(void) Motionbegan: (uieventsubtype) Motion withevent: (uievent *) event{ NSLog (@ "Start shaking");} Trigger when shaking-(void) motionended: (uieventsubtype) Motion withevent: (uievent *) event{ NSLog (@ "shaking End");} When the wobble is interrupted-(void) motioncancelled: (uieventsubtype) Motion withevent: (uievent *) event{ NSLog (@ "Undo Shake, Shake Stop");}
Daily Update Attention:Http://weibo.com/hanjunqiangSina Micro-blog
iOS in the gyroscope/speed Han Junqiang Blog