关于iOS屏幕旋转处理
首先Device orientation中要配置支持方向
其次关键方法如下:
1.是否支持自动旋转
- (BOOL)shouldAutorotate
2.返回支持旋转的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
支持正立竖屏
UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
支持左横屏
UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
支持右横屏
UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
支持倒立竖屏
UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
支持倒立横屏
UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
支持全部
UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
除了倒立竖屏都支持
UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
3.即将旋转时代理回调
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
4.完成旋转时代理回调
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
当转动屏幕后,手机会根据旋转方向进行主动旋转。
被动强制屏幕旋转:
如果想要强制旋转屏幕,比如点击按钮触发式旋转,方法如下:
(但这个方法不太亲和,可能会被拒或者与自动旋转冲突潜在bug)
NSNumber *resetOrientationTarget = [NSNumber numberWithInt: UIInterfaceOrientationUnknown];
[[UIDevice currentDevice] setValue: resetOrientationTarget forKey:@"orientation"];
NSNumber *orientationTarget = [NSNumber numberWithInt: UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue: orientationTarget forKey:@"orientation"];