Fun with the iPad accelerometers

May 29th, 2010 by Hugo

Here are the source files used during bilambee’s lightning talk at iPadMadCamp.

iPadAccelAppDelegate.m
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
  2.    
  3.     // Override point for customization after application launch.
  4.  
  5.     // Add the view controller's view to the window and display.
  6.     [window addSubview:viewController.view];
  7.     [window makeKeyAndVisible];
  8.  
  9.  
  10.  [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
  11.  
  12.  [[NSNotificationCenter defaultCenter] addObserver:viewController
  13.             selector:@selector(orientationChange)
  14.              name:@"UIDeviceOrientationDidChangeNotification" object:nil];
  15.  
  16.     return YES;
  17. }
iPadAccelViewController.h
  1. @interface iPadAccelViewController : UIViewController {
  2.  
  3. }
  4.  
  5. -(void)orientationChange;
  6.  
  7. @end
iPadAccelViewController.m
  1. -(void)orientationChange {
  2.  UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
  3.  
  4.  switch (orientation) {
  5.   case UIDeviceOrientationFaceUp:
  6.    NSLog(@"* FaceUp");
  7.    break;
  8.   case UIDeviceOrientationFaceDown:
  9.    NSLog(@"* FaceDown");
  10.    break;
  11.   case UIDeviceOrientationLandscapeLeft:
  12.    NSLog(@"* Left");
  13.    break;
  14.   case UIDeviceOrientationLandscapeRight:
  15.    NSLog(@"* Right");
  16.    break;
  17.   case UIDeviceOrientationPortrait:
  18.    NSLog(@"* Port");
  19.    break;
  20.   case UIDeviceOrientationPortraitUpsideDown:
  21.    NSLog(@"* PortDown");
  22.    break;
  23.   case UIDeviceOrientationUnknown:
  24.    NSLog(@"* Unknown");
  25.    break;  
  26.   default:
  27.    break;
  28.  }
  29.  
  30. }

Any questions?

Leave a reply