Principle of accelerometer in IOS

Source: Internet
Author: User

The accelerator is to use the power of the user on the iPhone device to measure its acceleration and gravity. The acceleration can be achieved in three directions:
X, Y, Z records, the number of records generated by (x, y, z) records.

(1) Whether the iPhone is facing up or back up

(2) whether the user is holding or holding (can be used with the auto-dynamic rotation function)

(3) How many distance does the Mobile Phone Touch, including upper, lower, and left

The acceleration operator is a single bit of G force (gravity). Normally, we will all be affected by gravity, that is,
G force: if you look at the iPhone in the normal upright way, you will get 1g of gravity in the Y axis, and then you will get the scattered G force in different directions, if your iPhone is quickly moved, the accelerator will be able to instantly change with a great deal of force to learn about the iPhone's movements.

When using the accelerator, you must create an accelerometerdelegate compliant
Uiaccelerometer is similar. uiaccelerometer exists in singleton mode, and then generates an example and makes the example a delegate to the accelerator, the program example for declaring an example is as follows:

UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];

You can set the update rate of the instance. You can use the following program to set the update rate, which indicates that the update is performed once every 30 seconds.

accelerometer.delegate = self;accelerometer.updateInterval  = 1.0f / 30.0f ;

After setting the delegate, you must implement the accelerometer: didaccelerate method. This method accepts two arguments. The first parameter is used.
Test the uiaccelerometer example, and the second is the uiaccleration object. The following method is to display the accelerated value that has been scaled to a label.

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{NSString *accelerateNum  = [[NSString alloc] initWithFormat:@" X: %g\tY:%g\tZ:%g", acceleration.x , acceleration.y, acceleration.z];label.text = accelerateNum ;[accelerateNum release];} 

If you want to handle the problem, you can use the following program

-(Void) accelerometer :( uiaccelerometer *) accelerometer didaccelerate :( uiacceleration *) Acceleration {If (fabsf (acceleration. x)> 1.0) {// indicates that something will happen when the X direction is greater than 1 }}
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: 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.