Search This Blog

Sep 9, 2011

iPhone Accelerometer Example.

This example shows how use accelerometer in iPhone.


You can download source code from here Download.

1. Code for .h file.

#import

@interface AccelerometerExampleViewController : UIViewController {
  
    UISlider *slider;
}

@end


2. Code for .m file.

#import "AccelerometerExampleViewController.h"

@implementation AccelerometerExampleViewController


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
  
    UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
    accel.updateInterval = 0.05f;
    accel.delegate = self;
  
    slider = [[UISlider alloc] initWithFrame:CGRectMake(10, 200, 300, 20)];
    slider.minimumValue = -1;
    slider.maximumValue = 1;
    [self.view addSubview:slider];
}


- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    NSLog(@"(%.02f, %.02f, %.02f)", acceleration.x, acceleration.y, acceleration.z);
    slider.value = acceleration.x;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc {
    [slider release];
    [super dealloc];
}

@end

8 comments:

  1. You have made a very good post for .h & .m files, by providing the source code download link. Really useful to use the accelerometer in iPhone.
    Download Shame Alarm iPhone App

    ReplyDelete
  2. yes
    it is usefull and steps and very good.
    thanks for sharing this info.
    have you created some social media applications ?
    if yes so please let me know that how can i do my self ?

    ReplyDelete
  3. Thanks guys, for sharing this conductive accumulation.
    discover more jailbreaking

    ReplyDelete
  4. Very Nice tutorial.....
    Rajesh

    ReplyDelete
  5. -------------------
    Your contents are too simple to read and easy to understand.
    --------------------------------
    iPhone App Development And
    iOS App Development

    ReplyDelete
  6. Thanks for your ideas. You can also find the details on Affity Solutions, at the Iphone Developers. The main object of the Affity Solutions is to provide quality web services and is among the few software development company in Nagpur.

    ReplyDelete
  7. Thanks for your ideas. You can also find the details on Affity Solutions, at the Iphone Developers. The main object of the Affity Solutions is to provide quality web services and is among the few software development company in Nagpur.

    ReplyDelete
  8. Very useful article for all iPhone app developer. I am sharing this article for friends also. Thanks
    enterprise social mobility, enterprise mobile apps

    ReplyDelete