iOS Development Accelerometer Uiaccelerometer

Source: Internet
Author: User

Uiaccelerometer has been marked as outdated in iOS5, but because it is very simple, it does not affect the way we learn to use it. After iOS5 we can use the Core Motion framework

What's the use of accelerometers?

    • Detecting the movement of a device

Application Scenarios

    • Shake a Shake
    • Pedometer

The principle of accelerometer

    • Detects the acceleration of the device on the X,Y,Z axes (which direction has a strong effect, which direction is moving)
    • According to the acceleration value, we can determine the intensity of the action in all directions.

Accelerometers have an acceleration range of 1 to 1, because the device is normally subject to gravity, so there will be a downward 1 acceleration in the above cases.

New project, we add the following code in Viewdidload

1     // Get Singleton objects 2     Uiaccelerometer *accelerometer = [Uiaccelerometer sharedaccelerometer]; 3     // Set up proxy 4     Accelerometer. delegate = self ; 5     // set the sampling interval 1/60.0 is 1 seconds to collect 60 times 6     1 60.0;

Implementing Proxy methods

1 -(void) Accelerometer: (Uiaccelerometer *) Accelerometer didaccelerate: (uiacceleration *) Acceleration2{3     NSLog (@ "x Acceleration%f--y Acceleration%f--z Acceleration%f" , acceleration.x,acceleration.y,acceleration.z); 4 }

The proxy method returns the accelerometer object and the result object, and the result of the run program is as follows

1  -- Geneva-Ten  One: One:06.168uiaccelerometer[1147: 60b] x acceleration-0.060181--y Acceleration 0.094147--z Acceleration-0.9889222  -- Geneva-Ten  One: One:06.186uiaccelerometer[1147: 60b] x acceleration-0.059189--y Acceleration 0.093430--z Acceleration-0.9879613  -- Geneva-Ten  One: One:06.204uiaccelerometer[1147: 60b] x acceleration-0.059387--y Acceleration 0.092224--z Acceleration-0.9874884  -- Geneva-Ten  One: One:06.221uiaccelerometer[1147: 60b] x acceleration-0.061752--y Acceleration 0.092514--z Acceleration-0.9872285  -- Geneva-Ten  One: One:06.239uiaccelerometer[1147: 60b] x acceleration-0.057999--y Acceleration 0.092453--z Acceleration-0.9889376  -- Geneva-Ten  One: One:06.257uiaccelerometer[1147: 60b] x acceleration-0.060883--y Acceleration 0.095367--z Acceleration-0.9850627  -- Geneva-Ten  One: One:06.275uiaccelerometer[1147: 60b] x acceleration-0.058121--y Acceleration 0.095779--z Acceleration-0.9887088  -- Geneva-Ten  One: One:06.292uiaccelerometer[1147: 60b] x acceleration-0.060349--y Acceleration 0.093201--z Acceleration-0.9867559  -- Geneva-Ten  One: One:06.310uiaccelerometer[1147: 60b] x acceleration-0.054764--y Acceleration 0.093338--z Acceleration-0.990158

We see that the device is flat, with an acceleration of about 1 in the negative direction of the z-axis.

First understand the use of the accelerometer, next we do a program, the screen has a small ball, you can move around with gravity, we draw a small ball on the screen, and the controller connected

1 @property (weak, nonatomic) Iboutlet Uiimageview *ball;

Let the ball motion is to modify the small ball x, y, how do we let x, Y and acceleration?

Junior physics We have learned, displacement = speed * time = acceleration * time * time, because we set the sample rate is 1/60, so each time the agent method is called 1/60 seconds, so the instantaneous speed of each moment is the accumulation of acceleration, and the displacement of each moment is the accumulation of speed, so the agent method we write

1 -(void) Accelerometer: (Uiaccelerometer *) Accelerometer didaccelerate: (uiacceleration *) Acceleration2{3     _volecity.x + = acceleration.x; 4     _volecity.y + = acceleration.y; 5     6     self.ball.x + = _volecity.x; 7     SELF.BALL.Y-= _volecity.y; 8 }

We set a property to record the speed

1 @property (assign, nonatomic) Cgpoint volecity;

Running the program, we found that the ball can actually do the motion, but also has the acceleration, but the ball will exceed the boundary, in the proxy method continues to add the following code

1     if(self.ball.x<=0){2self.ball.x =0;3     }4     if(self.ball.y<=0) {5SELF.BALL.Y =0;6     }7     if(self.ball.maxx>=self.view.width) {8Self.ball.maxX =Self.view.width;9     }Ten     if(self.ball.maxy>=self.view.height) { OneSelf.ball.maxY =Self.view.height; A}

The small ball is imprisoned in the box, and we add some code to allow the ball to bounce when it touches the border.

1     //Boundary Detection2     if(self.ball.x<=0){3self.ball.x =0;4         //add bounce, weaken speed5_volecity.x *=-0.5;6     }7     if(self.ball.y<=0) {8SELF.BALL.Y =0;9_volecity.y *=-0.5;Ten     } One     if(self.ball.maxx>=self.view.width) { ASelf.ball.maxX =Self.view.width; -_volecity.x *=-0.5; -     } the     if(self.ball.maxy>=self.view.height) { -Self.ball.maxY =Self.view.height; -_volecity.y *=-0.5; -}

Run the program we found that the ball can rebound, the effect is good, example code: Https://github.com/dongbobo1992/iOSExamples

iOS Development Accelerometer Uiaccelerometer

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.