Coremotion in-frame accelerometer using a concise tutorial

Source: Internet
Author: User

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

UIAccelerometerThe 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

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.