监听iOS检测屏幕旋转状态,不需开启屏幕旋转
- -(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较底层,在上面两个方法能够解决问题的情况下建议不要用,使用不当性能损耗非常大)。

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window
{
return UIInterfaceOrientationMaskPortrait;
}
参考链接 https://blog.csdn.net/nadeal/article/details/79542682
监听iOS检测屏幕旋转状态,不需开启屏幕旋转的更多相关文章
- 监听iOS检测屏幕旋转状态,不需开启屏幕旋转-b
-(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& ...
随机推荐
- bzoj2338数矩形(rectangle)
数矩形(rectangle) 计算几何 把所有点两两连线,把这些线按长度排序,再按中点排序 能组成矩形的线一定是连续的 最坏就是所有点围成一个圆,交于圆心 复杂度O(可以过) 要用叉积,不然会被卡精 ...
- 从jekyll转向hexo
当年选择jekyll是因为看中了HCZ Material theme这个主题,折腾了很久才把博客搭建好,后来周边人准备些博客的时候已经不推荐使用jekyll了,推荐hexo给好几个人,不用他们折腾,( ...
- 使用 swagger组件给asp.net webapi文档生成
1.名词解释 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件的方法,参数和模 ...
- Nature Reserve
Nature Reserve time limit per test:2 seconds memory limit per test:256 megabytes input:standard inpu ...
- div切换 div轮换显示
原文发布时间为:2009-05-10 -- 来源于本人的百度文章 [由搬家工具导入] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tran ...
- [LeetCode] Remove Duplicates from Sorted List II 链表
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- android中Adapter适配器的讲解
Adapter(适配器的讲解) 适配器就我自己来看,我觉得这是一个非常重要的知识点,Adapter是用来帮助填出数据的中间桥梁,简单点说吧:将各种数据以合适的形式显示在View中给用户看.Adapte ...
- 转:C#制作ORM映射学习笔记二 配置类及Sql语句生成类
在正式开始实现ORM之前还有一点准备工作需要完成,第一是实现一个配置类,这个很简单的就是通过静态变量来保存数据库的一些连接信息,等同于.net项目中的web.config的功能:第二需要设计实现一个s ...
- 2018年东北农业大学春季校赛 F wyh的集合【思维】
链接:https://www.nowcoder.com/acm/contest/93/F来源:牛客网 题目描述 你们wyh学长给你n个点,让你分成2个集合,然后让你将这n个点进行两两连接在一起,连接规 ...
- Codeforces Gym 101194C Mr. Panda and Strips(2016 EC-Final,区间DP预处理 + 枚举剪枝)
题目链接 2016 EC-Final 题意 现在要找到数列中连续两个子序列(没有公共部分).要求这两个子序列本身内部没有重复出现的数. 求这两个子序列的长度的和的最大值. 首先预处理一下.令$ ...