/**
* 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