From the Geek's Head

Hello Accelerometer

The question new devs ask me the most is “How do we use the iPhone accelerometer?”, so I have decided to write a post about how to get data from the iOS device’s accelerometer.

The accelerometer is a sensitive piece of hardware that can detect the changing position of the device, from translation motion to rotation; it has the capability to detect the changes using a 3D coordinate system. Using the accelerometer devs can enhance the UX of their apps considerably, similarly game developers can make their games more engaging with it. In this blog post I am gonna show you how to get the accelerometer coordinate values and display them inside a label. So lets get started.

First lets create a simple UI in the interface builder:

That’s that a simple and clean UI. Now lets get to some action, lets write some code. First we create some IBOutlet properties in out interface file for our labels:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property(nonatomic, strong) IBOutlet UILabel *xLabel;
@property(nonatomic, strong) IBOutlet UILabel *yLabel;
@property(nonatomic, strong) IBOutlet UILabel *zLabel;

@end

We are not done with the interface file yet, we need to implement the UIAccelerometer delegate and create a property to access its values. So after adding these our interface would look like this:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIAccelerometerDelegate>



@property(nonatomic, strong) IBOutlet UILabel *xLabel;
@property(nonatomic, strong) IBOutlet UILabel *yLabel;
@property(nonatomic, strong) IBOutlet UILabel *zLabel;

@property(nonatomic, strong) UIAccelerometer *accelerometer;


@end

So all that takes care of the interface. Now we need to setup the accelerometer object so it can actually be of use. We now move to the implementation file of our project and modify the viewDidLoad method as follows:

- (void)viewDidLoad
{
    [super viewDidLoad];
	
    self.accelerometer = [UIAccelerometer sharedAccelerometer];
    self.accelerometer.updateInterval = .2;
    self.accelerometer.delegate = self;
    
}

Important thing to note here is how we have initialized the accelerometer object, one should never initialize a new instance of this object as its a shared resource. This is the correct way to initialize it. The other two statements are how we setup the update interval of the accelerometer and tell it whose going to be its delegate(in this case its our viewcontroller).

We are almost done with this example, the final piece of the puzzle is UIAccelerometer’s delegate method didAccelerate. We implement that this way:

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
    
    self.xLabel.text = [NSString stringWithFormat:@"%f", acceleration.x];
    self.yLabel.text = [NSString stringWithFormat:@"%f", acceleration.y];
    self.zLabel.text = [NSString stringWithFormat:@"%f", acceleration.z];
}

Trust me thats basically all you need to read data from the accelerometer. Our project is complete. Source files are available on github and here is a video showing the project in action:

Thats it for now. I hope it proves helpful.

blog comments powered by Disqus