Sensor-Accelerometer-coremotion

Source: Internet
Author: User

/**

* Coremotion

*

*/

#import "ViewController.h"

#import <CoreMotion/CoreMotion.h>//import Framework

@interface Viewcontroller ()

@property (nonatomic, strong) Cmmotionmanager *mgr;//must be made into a global

@end

@implementation Viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

[Self push];

[self-pull];

}

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event

{

cmacceleration acceleration = self.mgr.accelerometerData.acceleration;

Logred (@ "%f---%f----%f", acceleration.x, ACCELERATION.Y, acceleration.z);

}

/**

* Pull---when needed, collect

*/

-(void) pull

{

1. Create a Motion Manager object

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

2. Determine if the accelerometer is available

if (self.mgr.isAccelerometerAvailable) {

4. Start Sampling---Pull

[Self.mgr startaccelerometerupdates];

}else{

Loggreen (@ "Accelerometer not available");

}

}

/**

* Push---Capture in real time based on the set acquisition interval

*/

-(void) push

{

1. Create a Motion Manager object

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

2. Determine if the accelerometer is available

if (self.mgr.isAccelerometerAvailable) {

/**

* Accelerometerupdateinterval---sampling time

Isaccelerometeractive---is collecting

Startaccelerometerupdates---Pull

Startaccelerometerupdatestoqueue---Push

Stopaccelerometerupdates---stop sampling

Accelerometerdata---data collected

*/

3. Set sampling interval

Self.mgr.accelerometerUpdateInterval = 1.0/30.0;

4. Start sampling

[Self.mgr startaccelerometerupdatestoqueue:[nsoperationqueue Mainqueue] withhandler:^ (CMAccelerometerData * Accelerometerdata, Nserror *error) {

When the data is collected, it is called

if (error) return;

cmacceleration acceleration = accelerometerdata.acceleration;

Logred (@ "%f---%f----%f", acceleration.x, ACCELERATION.Y, acceleration.z);

}];

}else{

Loggreen (@ "Accelerometer not available");

}

}

Sensor-Accelerometer-coremotion

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.