Tuesday, March 16, 2010

Beginning UIAccelerometer

UIaccelerometer automatically detects when the user has moved his/her iphone. It measures on 3 axis planes, and they are the following.

X: When the user moves iphone left/right
Y: When the user moves iphone forward/backward
Z: When the user moves the iphone up/down

# Create your Viewcontroller.h file

#import

//sensitivity
#define kAccelerometerFrequency 40

@interface simpleTiltViewController : UIViewController {

CGPoint deviceTilt;
}

@end

# Create your ViewController.m file

#import "simpleTiltViewController.h"

@implementation simpleTiltViewController

- (void)viewDidLoad {
[super viewDidLoad];



[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / kAccelerometerFrequency)];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
}

// UIAccelerometerDelegate method, called when the device accelerates.
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
// Update the accelerometer graph view
deviceTilt.x = acceleration.x;
deviceTilt.y = acceleration.y;
}

@end


Use these two files in your project

1 comment: