1) Start a new project in Xcode
2) Add file new file MainViewController.h and MainViewController.m with MainViewController.xib, by ticking the xib checkbox while adding.
3) Below is the xib file to be designed and make connections accordingly.
MainViewController.h
#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController <UIAccelerometerDelegate> {
IBOutlet UILabel *labelX;
IBOutlet UILabel *labelY;
IBOutlet UILabel *labelZ;
IBOutlet UIProgressView *progressX;
IBOutlet UIProgressView *progressY;
IBOutlet UIProgressView *progressZ;
UIAccelerometer *accelerometer;
}
@property (nonatomic, retain) IBOutlet UILabel *labelX;
@property (nonatomic, retain) IBOutlet UILabel *labelY;
@property (nonatomic, retain) IBOutlet UILabel *labelZ;
@property (nonatomic, retain) IBOutlet UIProgressView *progressX;
@property (nonatomic, retain) IBOutlet UIProgressView *progressY;
@property (nonatomic, retain) IBOutlet UIProgressView *progressZ;
@property (nonatomic, retain) UIAccelerometer *accelerometer;
@end
MainViewController.m
#import "MainViewController.h"
@implementation MainViewController
@synthesize labelX;
@synthesize labelY;
@synthesize labelZ;
@synthesize progressX;
@synthesize progressY;
@synthesize progressZ;
@synthesize accelerometer;
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self.accelerometer = [UIAccelerometer sharedAccelerometer];
self.accelerometer.updateInterval = .1;
self.accelerometer.delegate = self;
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
labelX.text = [NSString stringWithFormat:@"%@%f", @"X: ", acceleration.x];
labelY.text = [NSString stringWithFormat:@"%@%f", @"Y: ", acceleration.y];
labelZ.text = [NSString stringWithFormat:@"%@%f", @"Z: ", acceleration.z];
self.progressX.progress = ABS(acceleration.x);
self.progressY.progress = ABS(acceleration.y);
self.progressZ.progress = ABS(acceleration.z);
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
4) In .m file above the "didAccelerate" is an AccelerometerDelegate method.
5) Build and Run on device.
NOTE : Accelerometer does not works on Simulator.
No comments:
Post a Comment