Objective
Before someone made a mobile phone anti-theft app through the iphone's accelerometer, and it was through this anti-theft app to get Angel investment. From then on gorgeous turn, the company develops the vivid. Although this project is not very good at the end, but it is really a nice idea.
Maybe you don't know, every iphone has an accelerometer built in. This allows the built-in accelerometer to respond when the user is working on the phone, such as rotating the phone.
Before the iOS4, the accelerometer was UIAccelerometer
collected from the data, and is now used for CoreMotion
processing. Both uses are simple and relatively UIAccelerometer
simple. This article will share the usage of both.
Accelerometer principle
From the network
The iphone's accelerometer has three axes, namely the x-axis, y-axis, and z-axis, as shown in. This is a three-dimensional space that captures the actions of the user at every angle. With these three axes you can calculate the tilt angle of the iphone to calculate the acceleration.
Uiaccelerometer use
UIAccelerometer
The use of the more simple, need to implement UIAccelerometerDelegate
the proxy method, the specific code is as follows:
UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];accelerometer.delegate = self;accelerometer.updateInterval = 0.1;
To implement the UIAccelerometerDelegate
proxy method:
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration NS_DEPRECATED_IOS(2_0, 5_0){ NSLog(@"x -> %f y - > %f z -> %f",accelerometer.x,accelerometer.y,accelerometer.z);}
Coremotion Acceleration Meter
first need to introduce #import <coremotion/coremotion.h>
, directly through block
callback, then processes data for three axes. The code is as follows:
Cmmotionmanager *motionmanager = [[Cmmotionmanager alloc] init]; Nsoperationqueue *queue = [[Nsoperationqueue alloc] init];//Accelerometer if (motionmanager.accelerometeravailable) {MotionManag Er.accelerometerupdateinterval = 0.1; [Motionmanager startaccelerometerupdatestoqueue:queue withhandler:^ (Cmaccelerometerdata *accelerometerData, Nserror *error) {if (error) {[Motionmanager stopaccelerometerupdates]; NSLog (@ "error"); }else{NSLog (@ "x,%f y->%f z-%f", Accelerometerdata.acceleration.x,accelerometerdata.accelerati ON.Y,ACCELEROMETERDATA.ACCELERATION.Z); } }];} else{NSLog (@ "This device has no accelerometer");}
The above is about UIAccelerometer
and CoreMotion
the use of the accelerometer is really the highlight feature of the iphone. Specific use, we need to divergent thinking, maybe one day you also have a very cow idea. The cock thread may be in the next moment.
?
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Coremotion in-frame accelerometer using a concise tutorial