iPad の回転検出方法

回転の自動検出の仕方っていつも忘れちゃう。メモっとこ。:-)

@implementation RotatePageAppDelegate
@synthesize window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after application launch.
	//ノーティフィケーション登録。
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
	
	//通知開始
	[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [window makeKeyAndVisible];
    
    return YES;
}

- (void)deviceOrientationDidChange:(NSNotification*)notification {
	UIDeviceOrientation orientation;
	orientation = [UIDevice currentDevice].orientation;
	NSString *msg = nil;
	if(orientation == UIDeviceOrientationUnknown) {
		msg = @"不明"; 
	}
	if(orientation == UIDeviceOrientationPortrait) {
		msg = @"縦"; 
	}
	if(orientation == UIDeviceOrientationPortraitUpsideDown) {
		msg = @"縦(上下逆)"; 
	}
	if(orientation == UIDeviceOrientationLandscapeLeft) {
		msg = @"横(左側上)"; 
	}
	if(orientation == UIDeviceOrientationLandscapeRight) {
		msg = @"横(右側上)"; 
	}
	if(orientation == UIDeviceOrientationFaceDown) {
		msg = @"画面下向き"; 
	}
	NSLog(msg);
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	return YES;
}
…
@end