关于自动旋转

原文链接:http://www.devdiv.com/home.php?mod=space&uid=23234&do=blog&id=2921

做自动旋转有两套代码:
一是获取NSNotification ,然后去设置回调函数
二是,继承controller,重写shouldAutorotateToInterfaceOrientation方法

方法1:
在congtroller里面重写这个方法
这段代码表示,只是UIInterfaceOrientationPortraitUpsideDown的返回YES

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
		return YES;
	}
	return NO;
}
方法2:
以下这段代码表示,只是支持水平翻转
UIDevice *device = [UIDevice currentDevice];
    [device beginGeneratingDeviceOrientationNotifications];
	[[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(doRotate:)
                                                 name: @"UIDeviceOrientationDidChangeNotification"
                                               object: nil]; 
- (void)doRotate:(NSNotification *)notification
{
	CCDirector *director = [CCDirector sharedDirector];
	switch ([[UIDevice currentDevice] orientation]) {
		case CCDeviceOrientationLandscapeLeft:
			[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
			break;
		case CCDeviceOrientationLandscapeRight:
			[director setDeviceOrientation:kCCDeviceOrientationLandscapeRight];
			break;	
		default:
			break;
	}
}
特别要提出的是,director 获取的orientation和[[UIDevice currentDevice] orientation]是不同的,切记

你可能感兴趣的:(关于自动旋转)