IOS Gyroscope Development (Coremotion Framework) example detailed _ios

Source: Internet
Author: User

iOS Gyroscope parameter significance

Self.mmanager = [[Cmmotionmanager alloc]init];

  Self.mManager.deviceMotionUpdateInterval = 0.5;

  

  if (self.mManager.gyroAvailable) {

    [Self.mmanager startdevicemotionupdatestoqueue:[nsoperationqueue Currentqueue] withhandler:^ (cmdevicemotion * _nullable motion, Nserror * _nullable error) {

      NSLog (@ "Rotationrate X:% .2LF y:%.2lf Z:%.2lf ", motion.useracceleration.x,motion.useracceleration.y,motion.useracceleration.z);

    }];

  }

X axis head near negative y parameter
X-axis head away from positive numbers

Left high positive x parameter on y-axis
High negative right of Y axis

Below is a description of the Coremotion framework

Coremotion is a specially designed framework for motion, which contains two parts of accelerometers and gyroscopes, which are responsible for data acquisition by Uiaccelerometer before IOS4, and are now generally used to handle the acceleration process with coremotion, However, because the uiaccelerometer is relatively simple, the same people are using. The accelerometer is determined by three axes, user most common operation equipment movement, shaking mobile phone (shaking), tilt mobile phone can be detected by equipment, accelerometers can detect linear changes, gyro can better detect deflection of the action, can be based on the user's action to make the corresponding action, iOS simulator can not simulate the above action, the real machine debugging requires the developer account.

Acceleration meter

The accelerometer is x,y,z in three directions, refer to the following figure:

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

-(void) Viewdidload {
   //Request to turn on accelerometer and begin receiving Accelerometer events
   [[Uidevice Curr Entdevice] begingeneratingdeviceorientationnotifications];
   [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @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
   [[Nsnotifi Cationcenter Defaultcenter] removeobserver:self];
   [[Uidevice Currentdevice] endgeneratingdeviceorientationnotifications];
}

When the user shakes the device, the system notifies each device in use to make itself the first responder:

-(BOOL) Canbecomefirstresponder {return
  YES;
}
 
-(void) Viewdidappear: (BOOL) Animated {
  [self becomefirstresponder];
}

There are three ways to handle motion events, beginning (Motionbegan), ending (motionended), Canceling (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:@ "Flyelephant" object:self];
  }

Coremotion is a very important framework for the processing of accelerometer data and gyro data, and the framework itself integrates many algorithms to obtain native data and is well displayed, Coremotion and Uikit are connected uievent rather than event response chains. Coremotion is simply a more simple distribution motion event than receiving data.

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

Pull mode: Be able to obtain the current sensor status or combination data in Coremotionmanager read-only mode;

Push method: Collect the desired data in the form of block or closure and get real-time updates in a specific period;

Pull processing mode:

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

Touch End:

-(void) touchesended: (Nsset *) touches withevent: (uievent *) event{cmacceleration
   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 Accelerometer Frequency
[_motionmanager setaccelerometerupdateinterval:1/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 in fact the main method and way and the accelerometer is no different, first look at the gyro rotation angle Picture:

There are also two ways to

Gyroscope update data, Pull (startgyroupdates), push mode (startgyroupdatestoqueue):

static const Nstimeinterval gyromin = 0.01; -(void) Startupdateswithslidervalue: (int) Slidervalue {//Determine the update interval nstimeinterval delta = 0.0
   05;
 
   Nstimeinterval updateinterval = gyromin + Delta * slidervalue;  Create a cmmotionmanager Cmmotionmanager *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] Sh
   Aredmanager];
   if ([mmanager isgyroactive] = = YES) {[Mmanager stopgyroupdates];

 }
}

  Thank you for reading, I hope to help you, thank you for your support for this site!

Related Article

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.