关于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"];
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容