想要在IOS6下支持屏幕旋转,首先有一下两点要求:
1、在 Info.plist 中需要有Supported interface orientations支持(默认支持三个方向的旋转)
2、 在添加页面是,采取的是ViewController形式,而不是view
例如: AppDelegate中,[self.windows setRootViewController viewController ]
而不是: [self.windows.view addSubview: viewController.view]
接下来就可以设置屏幕支持了,ios默认只支持竖屏,不支持转屛
1、在viewControll.m 文件中添加函数,表明支持旋转屏幕
- (BOOL)shouldAutorotate
{
return YES;
}
这个函数的意思是,是否支持旋转屏幕,YES ,支持
2、 添加函数,表明支持具体哪几个屏幕
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
return的就是支持屏幕的类型,在IOS6之前,一般是返回UIInterfaceOrientation 类型,上次左右,简单明了
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
};
但是IOS6定义了新的类型UIInterfaceOrientationMask,定义如下:
typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
};
这个类型,不仅包括上下左右,还包括他们的不同合集,比如左右,上下左右,上左右等,我在函数中的返回值 UIInterfaceOrientationMaskAll 就是指,所有的旋转都支持
如此,旋转的设置便定义好了。
那么,如何准对旋转,来执行相应的方法呢?
我们便需要用到
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
在函数里,可以根据对应的UIInterfaceOrientation 来应对,举例如下:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
NSLog(@"left");
self.view.backgroundColor = [UIColor greenColor];
}
if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
self.view.backgroundColor = [UIColor whiteColor];
NSLog(@"center");
}
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
self.view.backgroundColor = [UIColor yellowColor];
NSLog(@"right");
}
}
这个Demo就是根据屏幕的旋转来变换不同的色,很简单,不解释。
源代码如下:屏幕旋转源代码