YouTip LogoYouTip

Ios Accelerometer

# iOS Accelerometer (accelerometer) * * * ## Introduction The accelerometer detects changes in the device's position along the x, y, and z axes. With the accelerometer, you can know the current position of the device relative to the ground. The following example code needs to be run on a real device; it will not work on the simulator. ### Example Steps 1. Create a simple view application 2. Add three labels to ViewController.xib and create ibOutlets for them: xlable, ylabel, and zlabel 3. Update ViewController.h as shown below #import @interface ViewController : UIViewController{ IBOutlet UILabel *xlabel; IBOutlet UILabel *ylabel; IBOutlet UILabel *zlabel;}@end 4. Update ViewController.m as shown below #import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { ; [setDelegate:self]; //Do any additional setup after loading the view,typically from a nib}- (void)didReceiveMemoryWarning { ; // Dispose of any resources that can be recreated.}- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate: (UIAcceleration *)acceleration{ [xlabel setText:[NSString stringWithFormat:@"%f",acceleration.x]]; [ylabel setText:[NSString stringWithFormat:@"%f",acceleration.y]]; [zlabel setText:[NSString stringWithFormat:@"%f",acceleration.z]];}@end ### Output When we run this application on an iPhone device, the output is as follows. ![Image 2: accelerometer_Output](#)
← Ios Universal ApplicationsAtt Ios Ui Switches β†’