【屏幕旋转】
@interface UIViewController (UIViewControllerRotation)
带研究。
+ (void)attemptRotationToDeviceOrientationNS_AVAILABLE_IOS(5_0);
[UIViewController attemptRotationToDeviceOrientation];//直接用?可以放在动画里面
// New Autorotation support.
- (BOOL)shouldAutorotateNS_AVAILABLE_IOS(6_0);//是否可以转屏
- (NSUInteger)supportedInterfaceOrientationsNS_AVAILABLE_IOS(6_0);//支持旋转的方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentationNS_AVAILABLE_IOS(6_0);//优先显示的旋转方向
1、全局控制
Info.plist文件中,有一个Supported interface orientations,可以配置整个应用的屏幕方向,此处为全局控制。
2、UIWindow
iOS6的UIApplicationDelegate提供了下述方法,能够指定 UIWindow 中的界面的屏幕方向:
该方法默认值为 Info.plist 中配置的 Supported interface orientations 项的值。
iOS中通常只有一个 window,所以此处的控制也可以视为全局控制。
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskPortrait;
/*
typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
UIInterfaceOrientationMaskPortrait = (正向),
UIInterfaceOrientationMaskLandscapeLeft = (home键在左边),
UIInterfaceOrientationMaskLandscapeRight = (home键在右边),
UIInterfaceOrientationMaskPortraitUpsideDown = (倒置),
UIInterfaceOrientationMaskLandscape = (home键在左边或者右边),
UIInterfaceOrientationMaskAll = (所以方向),
UIInterfaceOrientationMaskAllButUpsideDown = (除了倒置都可以),
};
*/
}
3、controller
只有以下两种情况:
当前controller是window的rootViewController
当前controller是modal模式的时,orientations相关方法才会起作用(才会被调用),当前controller及其所有的childViewController都在此作用范围内。
-(BOOL)shouldAutorotate{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAll;
/*
typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
UIInterfaceOrientationMaskPortrait = (正向),
UIInterfaceOrientationMaskLandscapeLeft = (home键在左边),
UIInterfaceOrientationMaskLandscapeRight = (home键在右边),
UIInterfaceOrientationMaskPortraitUpsideDown = (倒置),
UIInterfaceOrientationMaskLandscape = (home键在左边或者右边),
UIInterfaceOrientationMaskAll = (所以方向),
UIInterfaceOrientationMaskAllButUpsideDown = (除了倒置都可以),
};
*/
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationUnknown;
/*
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown,(非倒置)
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,(正向)
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,(倒置)
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,(home在左侧)
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft(home在右侧)
};
*/
}
}
4、最终支持的屏幕方向
前面所述的3种控制规则的交集就是一个controller的最终支持的方向;
如果最终的交集为空,在iOS6以后会抛出UIApplicationInvalidInterfaceOrientationException崩溃异常。
@end