iOS Development-coremotion Framework

Source: Internet
Author: User

Turn from:

Coremotion is a framework specifically for motion, which contains two parts of accelerometers and gyroscopes, which are used by the Uiaccelerometer class to collect data before iOS4, and are now generally treated with coremotion to process acceleration, But because Uiaccelerometer is relatively simple, it is also used by others. Accelerometer is determined by three axes, the user's most common operation of the device movement, shaking the phone (shake a shake), tilt the phone can be detected by the device, the accelerometer can detect linear changes, gyroscope can better detect the deflection of the action, according to the user's action to make the corresponding action, iOS simulator does not simulate the above actions, the real-machine debugging requires a developer account.

Acceleration meter

Accelerometer in x, Y, z three directions, reference:

If you only need to know the direction of the device, do not need to know the direction of the vector angle, then you can use Uidevice to operate, but also according to the direction of the line to judge, in particular, you can refer to the official Apple code:

-(void) Viewdidload {  //Request to turn on accelerometerandBegin receiving Accelerometer Events[[Uidevice Currentdevice] begingeneratingdeviceorientationnotifications];[[Nsnotificationcenter Defaultcenter]Addobserver:SelfSelector@selector (Orientationchanged:)name:uideviceorientationdidchangenotification  Object:nil];} -(void) orientationchanged: (nsnotification *) Notification { //respond to changes in device orientation}-(void) viewdiddisappear { //request To stop receiving accelerometer events and turn off Accelerometer [[nsnotificationcenter Defaultcenter] removeobserver: Self];  [[uidevice Currentdevice] Endgeneratingdeviceorientationnotifications];}           

When the user shakes the device, the system notifies each of the devices in use and can make itself a first responder:

-(BOOL) Canbecomefirstresponder {    return YES;}-(void) Viewdidappear: (bool) animated {    [self Becomefirstresponder];} 

There are three ways to handle motion events, start (Motionbegan), End (motionended), Cancel (motioncancelled):

-(void) Motionbegan: (uieventsubtype) Motion withevent: (uievent *)event Ns_available_ios (3_0);-(void ) motionended: (uieventsubtype) Motion withevent: (uievent *)event Ns_available_ios (3_0);-(void) Motioncancelled: (Uieventsubtype) Motion withevent: (uievent *)event Ns_available_ios (3_0);    

The Motionended method handles:

-(void) motionended: (uieventsubtype) Motion withevent: (uievent *)event {    if (motion = = Uieventsubtypemotionshake)    {        //flyelephant  Http://www.cnblogs.com/xiaofeixiang        [[ Nsnotificationcenter Defaultcenter] Postnotificationname:object:self]; }}

Coremotion is a very important framework when dealing with accelerometer data and gyroscope data, the framework itself integrates many algorithms to obtain native data, and it can be well demonstrated that coremotion is different from Uikit, and is connected uievent rather than event response chains. Coremotion is simply a simpler distribution of motion events than receiving data.

The Cmmotionmanager class is able to use all of the mobile data for the device (motion data), and the Core motion Framework provides two ways to manipulate motion data:

Pull mode: Can be Coremotionmanager read-only method to obtain the current status of any sensor or combination of data;

Push mode: is to collect the desired data in blocks or closures and get real-time updates in a specific cycle;

Pull processing mode:

Determine if the accelerometer is available    if ([_motionmanager isaccelerometeravailable]) {        //Set the accelerometer sampling frequency        [_motionmanager Setaccelerometerupdateinterval:40.0];        [_motionmanager startaccelerometerupdates];    else {NSLog (@ "blog Park-flyelephant");}    

Touch End:

*) event{     cmacceleration acceleration=_motionmanager.accelerometerdata.acceleration;    NSLog (@ "%f---%f---%f", acceleration.x,acceleration.y,acceleration.z);}    

Push processing mode:

 @property (strong,nonatomic) cmmotionmanager * Motionmanager;  @property (strong,nonatomic) nsoperationqueue *quene;    
_motionmanager=[[cmmotionmanager Alloc]init]; //Determine if the accelerometer is available if ([_motionmanager isaccelerometeravailable]) {  //Set the accelerometer frequency   [_motionmanager Setaccelerometerupdateinterval:40.0]; //Start sampling data [_motionmanager startaccelerometerupdatestoqueue:_quene withhandler:^ (Cmaccelerometerdata * Accelerometerdata, Nserror *error) { NSLog (@ "%f---%f", accelerometerdata.acceleration.x, ACCELEROMETERDATA.ACCELERATION.Y); else { NSLog (@ "blog Park-flyelephant");}            

Time Setting Frequency:

Gyroscope

Gyroscope actually main method and way and accelerometer no difference, first look at the gyroscope rotation angle Picture:

There are two ways to update the gyroscope data, pull mode (startgyroupdates), push mode (startgyroupdatestoqueue):

StaticConst Nstimeinterval Gyromin =0.01;-(void) Startupdateswithslidervalue: (int) Slidervalue { Determine the update intervalNstimeinterval Delta =0.005;Nstimeinterval updateinterval = gyromin + Delta * slidervalue; Create a CmmotionmanagerCmmotionmanager *mmanager = [(Aplappdelegate *) [[UIApplication sharedapplication]Delegate] Sharedmanager];Aplgyrographviewcontroller * __weak weakself = self; Check whether the gyroscope is available if ([mmanager isgyroavailable] = = YES) {  Assign the update interval to the motion manager [Mmanager Setgyroupdateinterval:updateinterval]; [Mmanager startgyroupdatestoqueue:[nsoperationqueue Mainqueue] withhandler:^ (cmgyrodata *gyroData, NSError *error) {   [Weakself.graphview addx:gyrodata.rotationrate.x y:gyrodata.rotationrate.y z: GYRODATA.ROTATIONRATE.Z];    [weakself setlabelvaluex:gyrodata.rotationrate.x Y:gyrodata.rotationrate.y Z:gyrodata.rotationrate.z];  }]; } Self.updateIntervalLabel.text = [NSString stringwithformat:@ "%f", UpdateInterval];} -(void) stopupdates{ Cmmotionmanager *mmanager = [(Aplappdelegate *) [[ UIApplication Sharedapplication] delegate] sharedmanager];  if ([mmanager isgyroactive] = = YES) {  [Mmanager stopgyroupdates]; }              /span>   

iOS Development-coremotion Framework

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.