监听iOS检测屏幕旋转状态,不需开启屏幕旋转-b
- -(void)rotation_icon:(float)n {
- UIButton *history_btn= [self.view viewWithTag:<#(NSInteger)#>][self.view viewWithTagName:@"home_history"];
- UIButton *cam_btn = [self.view viewWithTagName:@"cam_btn"]; UIButton *cut_btn = [self.view viewWithTagName:@"cut_btn"]; UIButton *light_btn=[self.view viewWithTagName:@"light_btn"];
- history_btn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0);
- cam_btn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0);
- cut_btn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0);
- light_btn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0);
- }
- - (void)orientationChanged:(NSNotification *)note { UIDeviceOrientation o = [[UIDevice currentDevice] orientation];
- switch (o) {
- case UIDeviceOrientationPortrait: // Device oriented vertically, home button on the bottom
- [self rotation_icon:0.0];
- break;
- case UIDeviceOrientationPortraitUpsideDown: // Device oriented vertically, home button on the top
- [self rotation_icon:180.0];
- break;
- case UIDeviceOrientationLandscapeLeft: // Device oriented horizontally, home button on the right
- [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
- [self rotation_icon:90.0*3];
- break;
- case UIDeviceOrientationLandscapeRight: // Device oriented horizontally, home button on the left
- [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];
- [self rotation_icon:90.0];
- break;
- default:
- break;
- }
- }
- -(void)viewWillDisappear:(BOOL)animated {
- NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
- UIDevice *device = [UIDevice currentDevice]; //Get the device object
- [nc removeObserver:self name:UIDeviceOrientationDidChangeNotification object:device];
- }
- - (void)viewDidAppear:(BOOL)animated {
- // Do any additional setup after loading the view from its nib.
- //----- SETUP DEVICE ORIENTATION CHANGE NOTIFICATION -----
- UIDevice *device = [UIDevice currentDevice]; //Get the device object
- [device beginGeneratingDeviceOrientationNotifications]; //Tell it to start monitoring the accelerometer for orientation
- NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; //Get the notification centre for the app
- [nc addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:device];
在特别的场景下,需要针对屏幕旋转作特殊处理。在ios系统下实现相关的功能还是比较方便的。
我下面介绍两种方法:
1.注册UIApplicationDidChangeStatusBarOrientationNotification通知(举例:在一个viewcontroller类的viewdidload中注册该通知),示例代码如下:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationChange:)name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
- (void)statusBarOrientationChange:(NSNotification *)notification
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationLandscapeRight) // home键靠右
{
//
}
if (
orientation ==UIInterfaceOrientationLandscapeLeft) // home键靠左
{
//
}
if (orientation == UIInterfaceOrientationPortrait)
{
//
}
if (orientation == UIInterfaceOrientationPortraitUpsideDown)
{
//
}
}
注意这种方式监听的是StatusBar也就是状态栏的方向,所以这个是跟你的布局有关的,你的布局转了,才会接到这个通知,而不是设备旋转的通知。
当我们关注的东西和布局相关而不是纯粹设备旋转,我们使用上面的代码作为实现方案比较适合。
2.注册UIDeviceOrientationDidChangeNotification通知(举例:我们同样在一个viewcontroller类的viewdidload中注册该通知),示例代码如下:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
- (void)orientChange:(NSNotification *)noti
{
NSDictionary* ntfDict = [noti userInfo];
UIDeviceOrientation orient = [UIDevice currentDevice].orientation;
/*
UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait, // Device oriented vertically, home button on the bottom
UIDeviceOrientationPortraitUpsideDown, // Device oriented vertically, home button on the top
UIDeviceOrientationLandscapeLeft, // Device oriented horizontally, home button on the right
UIDeviceOrientationLandscapeRight, // Device oriented horizontally, home button on the left
UIDeviceOrientationFaceUp, // Device oriented flat, face up
UIDeviceOrientationFaceDown // Device oriented flat, face down */
switch (orient)
{
case UIDeviceOrientationPortrait:
break;
case UIDeviceOrientationLandscapeLeft:
break;
case UIDeviceOrientationPortraitUpsideDown:
break;
case UIDeviceOrientationLandscapeRight:
break;
default:
break;
}
}
注意到这种方式里面的方向还包括朝上或者朝下,很容易看出这个完全是根据设备自身的物理方向得来的,当我们关注的只是物理朝向时,我们通常需要注册该通知来解决问题(另外还有一个加速计的api,可以实现类似的功能,该api较底层,在上面两个方法能够解决问题的情况下建议不要用,使用不当性能损耗非常大)。
监听iOS检测屏幕旋转状态,不需开启屏幕旋转-b的更多相关文章
- 监听iOS检测屏幕旋转状态,不需开启屏幕旋转
-(void)rotation_icon:(float)n { UIButton *history_btn= [self.view viewWithTag:<#(NSInteger)#>] ...
- 短信状态监听 - iOS
当使用 App 时若短信介入需要对当前状态进行监听操作,根据不同的状态实行相关的需求操作,废话不多说步骤如下. 首先,常规操作先引用对应的头文件,来为后续功能铺路. #import <Messa ...
- 截屏状态监听 - iOS
既接到电话状态监听的需求之后再次添加了截屏状态的监听,当使用 App 时若用户执行截屏操作需要对当前状态进行监听操作,下面有两种方法,其中可以替换截屏的图片内容(Plan A),也可以弹出提示框(Pl ...
- iOS 实时监听app的网络连接状态
实际iOS开发中,在网络通信中我们大部分使用第三方(只谈短链),譬如 AFNetworking.ASIHttpRequest(这个停更了,想必现在没多少人用),swift的 Alamofire 等. ...
- 电话状态监听 - iOS
今天接到一个监听状态的需求,当使用 App 时若电话介入需要对当前状态进行监听操作(注:并非通话内容),根据不同的状态实行相关的需求操作,废话不多说步骤如下. 首先,常规操作先引用对应的头文件,来为后 ...
- 监听列表ListVIew的滑动状态
/*监听列表的滑动状态:暂时用不到 * SCROLL_STATE_FLING 时让图片不显示,提高滚动性能让滚动小姑更平滑 * SCROLL_STATE_IDLE 时显示当前屏幕可见的图片*/ mLi ...
- Android监听外部存储设备的状态(SD卡、U盘等等)
近期在项目中须要对外部存储设备的状态进行监听,所以整理了此笔记,以便日后查看. 外部存储设备的状态变化时发出的广播 对照不同状态下的广播 1. 插入外部SD卡时: 2. 移除外部SD卡时: 3. 连接 ...
- Android开发——监听Android手机的网络状态
0. 前言 在Android开发中监听手机的网络状态是一个常见的功能,比如在没网的状态下进行提醒并引导用户打开网络设置,或者在非wifi状态下开启无图模式等等.因此本篇将网上的资料进行了整理总结,方便 ...
- 监听事件动态改变dom状态
html代码: <table class="table table-striped"> <thead> <tr> <th>分类ID& ...
随机推荐
- 用Python和FFmpeg查找大码率的视频文件
用Python和FFmpeg查找大码率的视频文件 本文使用Python2.7, 这个工作分两步 遍历目录下的视频文件 用ffprobe获取是视频文件的码率信息 用ffprobe 获取json格式的视频 ...
- Linux--------------安装mysql(2)
在 CentOS7 上安装 MySQL5.7 1 通过 SecureCRT 连接到阿里云 CentOS7 服务器: 2 进入到目录 /usr/local/ 中:cd /usr/local/ 3 创建目 ...
- codeblocks 更改颜色主题
Code::Blocks是一款优秀的C/C++编辑器,但默认的颜色为白底黑字,并且没有自带更多的主题配置.因此,各种颜色主题需要手动配置. 首先关闭codeblocks软件. 下载文件colour_t ...
- 搭建FTP+PAM+MySQL环境
搭建FTP+PAM+MySQL环境 1 搭建环境: CentOS6.5或CentOS6.7 [root@vhost3 ~]# uname -a Linux vhost3 -.el6.x86_64 # ...
- 过滤所有的HTML标签
<script type="text/javascript"> var str = "<p style=color:#FF0000>恩恩,就是就是 ...
- (转)CSS字体大小: em与px、pt、百分比之间的对比
CSS样式最混乱的一个方面是应用程序中文本扩展的font-size属性.在CSS中,你可以用四个不同的单位度量来显示在web浏览器中的文本 大小.这四个单位哪一种最适合Web? 这个问题引起了广泛的争 ...
- xml中使用foreach遍历对象
如果是一个带数据的List对象 <select id="selectProductMSTList" resultType="java.util.Map" ...
- Java 泛型类型的一些限制
由于泛型类型在运行时被消除,因此,对于如何使用泛型类型是有一些限制的. 限制1:不能使用new E() 不能使用泛型类型参数创建实例.例如,下面的语句是错误的: E object = new E(); ...
- 二、 What's Maven,How to learning?
1. 哈哈,什么是Maevn, ←_←|| ?我怎么知道,来看看官方解释, Apache Maven is a software project management and comprehensio ...
- IBM WebSphere MQ 通道类型配置
IBM WebSphere MQ 通道类型配置 初学MQ,四种常见通道,windows下操作 目录 Sender--Receiver Server-Receiver Server-Requester ...