iOS in the gyroscope/speed Han Junqiang Blog

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.