iOS 全局竖屏 单个viewcontroller点击按钮支持横屏
注:由于当时的需求需要本文描述的方法只是视图旋转并不是真正的自动旋屏(不建议采用),末尾的第二个demo才是真正的自动旋屏且已正常上线APP,亲测有效。
问题描述:项目工程只支持竖屏,在播放器页面需要点击按钮进行横竖屏切换,并能根据手机的方向进行自动旋转
如图:只勾选了竖屏

解决方法:(主要是采用视图transform的原理 横屏时调整视频视图上的每个控件坐标位置 竖屏在调整回去)
1.电池状态栏的显示与隐藏
首先在plist文件中添加View controller-based status bar appearance 设置为no
然后在控制器中通过[[UIApplication sharedApplication] setStatusBarHidden: withAnimation:NO]方法
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:NO];
来显示或者隐藏状态栏,一般情况下竖屏时状态栏会显示 横屏则隐藏
导航栏也进行隐藏换成自己的头部视图条
self.navigationController.navigationBarHidden = YES;
2.通知监测状态栏方向(自动旋转屏幕)
设置全局的orientation:
orientation = [UIDevice currentDevice].orientation;
通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
方法:
- (void)orientChange:(NSNotification *)noti {
// UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
orientation = [UIDevice currentDevice].orientation;
NSLog(@"打印现在的设备方向:%ld",(long)orientation);
switch (orientation)
{
case UIDeviceOrientationPortrait: {
NSLog(@"屏幕竖直");
isFullScreen = NO;
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:NO];
// [UIView animateWithDuration:0.25 animations:^{
// playView.transform = CGAffineTransformMakeRotation(0);
// playView.frame = CGRectMake(0, 0, widthAll, 300);
// topView.frame = CGRectMake(0, 0, widthAll, 50);
// footView.frame = CGRectMake(0, 250, widthAll, 50);
// menuBtn.frame = CGRectMake(widthAll-100, 10, 100, 30);
// }];
[self shupinAction];
}
break;
case UIDeviceOrientationLandscapeLeft: {
isFullScreen = YES;
NSLog(@"屏幕向左转");
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];
[self rightHengpinAction];
// [UIView animateWithDuration:0.25 animations:^{
// playView.transform = CGAffineTransformMakeRotation(M_PI*0.5);
// playView.frame = CGRectMake(0, 0, widthAll, heightAll);
// topView.frame = CGRectMake(0, 0, heightAll, 50);
// footView.frame = CGRectMake(0, widthAll-50, heightAll, 50);
// menuBtn.frame = CGRectMake(heightAll-100, 10, 100, 30);
// }];
}
break;
case UIDeviceOrientationLandscapeRight: {
isFullScreen = YES;
NSLog(@"屏幕向右转");
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];
[self leftHengpinAction];
// [UIView animateWithDuration:0.25 animations:^{
// playView.transform = CGAffineTransformMakeRotation(-M_PI*0.5);
// playView.frame = CGRectMake(0, 0, widthAll, heightAll);
// topView.frame = CGRectMake(0, 0, heightAll, 50);
// footView.frame = CGRectMake(0, widthAll-50, heightAll, 50);
// menuBtn.frame = CGRectMake(heightAll-100, 10, 100, 30);
// }];
}
break;
default:
break;
}
}
3.点击按钮进行横竖屏切换
添加按钮,设置点击方法
-(void)rotateScreeen:(UIButton *)btn
{
NSLog(@"旋转屏幕");
btn.selected = !btn.selected;
if (btn.selected) {
isFullScreen = YES;
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO]; if (orientation == UIDeviceOrientationPortrait) {
NSLog(@"当前竖屏模式");
[self rightHengpinAction];
}else if(orientation == UIDeviceOrientationLandscapeLeft)
{
NSLog(@"当前左横屏模式");
[self shupinAction];
}else if(orientation == UIDeviceOrientationLandscapeRight)
{
NSLog(@"当前右横屏模式");
[self shupinAction];
} }else{
isFullScreen = NO;
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:NO];
[self shupinAction]; } }
4.程序运行效果
竖屏效果:

横屏效果:

简单的demo下载地址(未作ui):http://download.csdn.net/user/wusangtongxue
(更新)第二次修改后的真正横屏demo地址:http://download.csdn.net/download/wusangtongxue/10049935
iOS 全局竖屏 单个viewcontroller点击按钮支持横屏的更多相关文章
- iOS设置竖屏,播放视频可以任性旋转的解决方法,亲测可用
之前在网上找了很多方法,都是强制横屏,但是如果设备关闭旋转锁定,强制横屏后把设备竖立起来,播放器也会跟着竖过来,但是就回不去了.现在项目要求让app默认都是竖屏,只有在全屏播放的时候可以自由旋转,于是 ...
- ios手机竖屏拍照图片旋转90°问题解决方法
手机拍照会给图片添加一个Orientaion信息(即拍照方向),如下: 用ios手机拍照,系统会给图片加上一个方向的属性, ios相机默认的拍照方向是后摄Home键在右为正,前摄Home键在左为正. ...
- COCOS2D-X暂时设置竖屏,过一阵子再设置回横屏
mainActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏 竖屏是JAVA代码 另外再改动C ...
- iOS 实现单个页面支持横竖屏,其他页面只能竖屏
最近在自己的项目里面 有需要做一个需求 : app中某一个页面支持横竖屏, 而其他页面只能竖屏. 1 2 实现方法如下: 1 首先需要Xcode中选中支持的屏幕方向 2 Appdelegate中 . ...
- IOS竖屏应用单个页面横屏的解决办法
昨天朋友问我,怎么实现在竖屏的应用里,显示一个横屏的应用,由于也没做过 就说不知道了,但是我觉得会有这样的API ,因为我手机里就安装有这种类型的软件 今天早上起来,就想做一个Demo出来,惯例的是查 ...
- mui 横屏 竖屏
在项目中只有某个页面需要横屏 ,其他的都是竖屏展示的. 假设a页面横屏 ,返回之后竖屏 b页面 a+ 将其设置为横屏显示: b+ 将其设置为竖屏显示 但是进入a页面之后再返回b页面时 b页面也会称为横 ...
- HTML5中判断横屏竖屏
在移动端中我们经常碰到横屏竖屏的问题,那么我们应该如何去判断或者针对横屏.竖屏来写不同的代码呢. 这里有两种方法: 一:CSS判断横屏竖屏 写在同一个CSS中 1 2 3 4 5 6 @media s ...
- Android时时监測手机的旋转角度 依据旋转角度确定在什么角度载入竖屏布局 在什么时候载入横屏布局
一.场景描写叙述: 最近开发中遇到个问题,就是我们在做横竖屏切换的功能时.横竖屏布局是操作系统去感知的,作为开发员没法确定Activity在什么时候载入横屏布局,在什么时候载入竖屏布局.因此为了找到载 ...
- ionic2/3 禁止屏幕旋转,禁止横屏,竖屏
ionic2/ionic3禁止屏幕旋转,及解除禁止旋转 1.添加插件: cmd到项目目录---> cordova plugin add cordova-plugin-screen-orienta ...
随机推荐
- LeetCode283:Move Zeros
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- window 方法:延时 和 重复
window 方法 var timer = setTimeout(closeFunc,10*1000); function closeFunc(){ alert('close'); }
- linux自启动服务方式
方式一: /etc/init.d/servicename restart 编写 /etc/init.d/ 下面的标准的服务shell脚本 方式二: vim /etc/rc.local /home/ ...
- JPA 原生态SQL 的复杂查询之createNamedQuery
JPA 原生态SQL 的复杂查询之createNamedQuery调用存储过程,返回的List字段对应的填充实体============实体类,调用存储过程====================== ...
- C++STL学习笔记_(3)stack
10.2.4stack容器 Stack简介 ² stack是堆栈容器,是一种"先进后出"的容器. ² stack是简单地装饰deque容器而成为另外的一种容器. ² #inc ...
- wchar_t 和 char 之间转换
vc++2005以后,Visual studio 编译器默认的字符集为Unicode.VC中很多字符处理默认为宽字符wchar_t,如CString的getBuffer(),而一些具体操作函数的输入却 ...
- (剑指Offer)面试题32:从1到n整数中1出现的次数
题目: 输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数.例如输入12,从1到12这些整数中包含1的数字有1,10,11和12,一共出现了5次. 思路: 1.累加法 累加1到n中每个整数 ...
- Freescale OSBDM JM60仿真器
OSBDM-JM60 - 9S08JM60 Based OSBDM — It includes interfaces and firmware applied to all the targets s ...
- JavaScript的角色巨变和Web技术的发展
曾经JavaScript是职业程序员看不上眼的脚本语言,如今只有高级程序员才能驾驭它. JavaScript性质和地位的天翻地覆,正是Web技术飞速变化的印证. 最初职业程序员轻视JavaScript ...
- 稀疏自动编码之反向传播算法(BP)
假设给定m个训练样本的训练集,用梯度下降法训练一个神经网络,对于单个训练样本(x,y),定义该样本的损失函数: 那么整个训练集的损失函数定义如下: 第一项是所有样本的方差的均值.第二项是一个归一化项( ...