iOS Plus 特定页面横屏退出或切换其他页面再进入仍保持横屏
来源:互联网 发布:后盾网js视频教程下载 编辑:程序博客网 时间:2024/05/21 14:45
http://www.jianshu.com/p/f917df3e3b2e
最近在研究播放器,播放器需要根据手机旋转来切换横竖屏状态,且横竖屏都是同一个VC只是做了屏幕尺寸适配,全屏播放页面有一个分享按钮,参考其他APP要实现,分享到微博微信等强制竖屏APP之后回来仍旧保持横屏,而不是竖屏再切换回横屏。如下图所示。
APP所有VC横屏或竖屏
AppDelegate.m中有如下方法可以设置整个app的方向。
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { return UIInterfaceOrientationMaskLandscape;}
该方法实质上是制定window的方向,虽然一个app有多个VC,但是window都只有一个,所以这个指定了window的方向,那所有VC的方向就都是统一的。
特定VC横屏或竖屏
在iOS6之后,需要在某个页面强制横屏,可以使用以下代码:
- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation { return (toInterfaceOrientation == UIInterfaceOrientationMaskLandscape);}- (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape;}
此段代码针对于在特定VC指定横竖屏方向,但是由于我的demo中横竖屏都是同一个VC,这个方法就不能采用了。
特定状态横屏且window横屏
如果需要app进入其他app再回来或退出后重新进入都保持原来的方向,那么就需要改变window的方向,就需要用到APPDelegate中的supportedInterfaceOrientationsForWindow方法。只在特定状态横屏,那就需要一个标识符来判定当前是否需要横屏。代码如下:
//AppDelegate.h@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (nonatomic, assign) BOOL isLandscape;@end//AppDelegate.m- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { if (_ isLandscape) { return UIInterfaceOrientationMaskLandscape; } else { return UIInterfaceOrientationMaskAllButUpsideDown; }}//Player.m- (void)backToPortrait//回到竖屏{ AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; appDelegate.allowRotation = NO;}- (void)enterToLandscape//进入横屏{ AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; appDelegate.allowRotation = YES;}
但是发现加入以上代码,导致全屏模式下无法通过旋转手机方向进入回到竖屏模式了,看了一下是因为isLandscape为YES,所以始终return UIInterfaceOrientationMaskLandscape。最终修改AppDelegate.m中代码如下:
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { if (_isLandscape) { UIDeviceOrientation orientation = [UIDevice currentDevice].orientation; if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) { return UIInterfaceOrientationMaskLandscape; }else { // 横屏后旋转屏幕变为竖屏 return UIInterfaceOrientationMaskPortrait; } } else { return UIInterfaceOrientationMaskAllButUpsideDown; }}
此处需要注意,判断屏幕方向只能使用UIDeviceOrientation手机设备方向而不能用UIInterfaceOrientation状态栏方向,因为横屏状态下,走isLandscape逻辑,不改变当前VC方向,所以虽然手机方向改变,但状态栏方向不变,用状态方向判断会导致屏幕无法旋转。
原文链接:http://www.jianshu.com/p/f917df3e3b2e
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
- iOS Plus 特定页面横屏退出或切换其他页面再进入仍保持横屏
- iOS 个别页面强制横屏,其他页面竖屏
- iOS 个别页面强制横屏,其他页面竖屏
- iOS 个别页面强制横屏,其他页面竖屏
- iOS 项目中,特定页面强制横屏
- storyboard切换程序进入页面
- iOS页面之间切换
- IOS页面切换
- iOS 页面切换控制
- iOS UIWebView 全屏播放视频横屏 app其他页面不支持横屏
- 退出后点击浏览器后退不能回到成功页面;直接进入页面和跳转进入页面样式不同;防止直接输入页面地址或servlet;当servlet跳servlet时,进行其他操作后后退出现错误!
- ios导航实现页面切换
- IOS 视图切换 页面跳转
- ios开发切换页面特效
- IOS页面切换过渡动画
- IOS开发之页面切换
- IOS开发之页面切换
- IOS Storyboard页面切换详解
- java 中Lock的使用
- 前端程序员应该知道的 15 个 jQuery 小技巧
- flash as3.0 五子棋实现保存功能
- Adroid之NavigationView
- android 定时任务 Alarm机制
- iOS Plus 特定页面横屏退出或切换其他页面再进入仍保持横屏
- Android UI设计之<十四>自定义ViewGroup,实现绚丽的仿支付宝咻一咻雷达脉冲效果
- Oracle AWR(Automatic workload repository)
- jsonp多次请求报错 not a function的解决方法
- 养成在活动中自定义启动该活动的方法 startAction( )的习惯
- 使用 RMI + ZooKeeper 实现远程调用框架
- 洛谷 P1498 南蛮图腾 题解报告
- Android Studio报错:Error:duplicate files during packaging of APK
- C++中static的用法详解