ios 检测屏幕方向
方法一:通知中心监听
name:
// UIDeviceOrientationDidChangeNotification 允许方向改变的情况下,监听设备方向,与电池条无关
// UIApplicationDidChangeStatusBarOrientationNotification 允许方向改变的情况下,监听电池条方向
-(void)notifitionSatatus{
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[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)//home在下
{
}
if (orientation == UIInterfaceOrientationPortraitUpsideDown)
{
}
NSLog(@"%ld",orientation);
}
注意:在没有打开屏幕旋转的情况下,该通知无效
另外:viewcontrller内部控制及代理
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
} - (BOOL)shouldAutorotate
{
return YES;
} - (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
//
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
NSLog(@"UIViewController will rotate to Orientation: %d", toInterfaceOrientation);
} - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
NSLog(@"did rotated to new Orientation, view Information %@", self.view);
}
appdelegate中有相应改变其旋转方向的方法
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (self.allowRotation) {
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}
return UIInterfaceOrientationMaskPortrait;
}
方法二:陀螺仪监测
- (void)startMotionManager{
if (_motionManager == nil) {
_motionManager = [[CMMotionManager alloc] init];
}
//检测时间间隔
_motionManager.deviceMotionUpdateInterval = 1.0;
if (_motionManager.deviceMotionAvailable) {
NSLog(@"Device Motion Available");
[_motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]
withHandler: ^(CMDeviceMotion *motion, NSError *error){
[self performSelectorOnMainThread:@selector(handleDeviceMotion:) withObject:motion waitUntilDone:YES];
}];
} else {
NSLog(@"No device motion on device.");
[self setMotionManager:nil];
}
}
- (void)handleDeviceMotion:(CMDeviceMotion *)deviceMotion{
double x = deviceMotion.gravity.x;
double y = deviceMotion.gravity.y;
if (fabs(y) >= fabs(x))
{
if (y >=){
//UIDeviceOrientationPortraitUpsideDown;
[self.delegate actionWithDeviceOrientation:UIDeviceOrientationPortraitUpsideDown];
}
else{
// UIDeviceOrientationPortrait;
[self.delegate actionWithDeviceOrientation:UIDeviceOrientationPortrait];
}
}
else
{
if (x >= ){
// UIDeviceOrientationLandscapeRight;
[self.delegate actionWithDeviceOrientation:UIDeviceOrientationLandscapeRight];
}
else{
// UIDeviceOrientationLandscapeLeft;
[self.delegate actionWithDeviceOrientation:UIDeviceOrientationLandscapeLeft];
}
}
}
//停止检测
-(void)endMotionManager{
[_motionManager stopDeviceMotionUpdates];
}
ios 检测屏幕方向的更多相关文章
- 监听iOS检测屏幕旋转状态,不需开启屏幕旋转-b
-(void)rotation_icon:(float)n { UIButton *history_btn= [self.view viewWithTag:<#(NSInteger)#>] ...
- 监听iOS检测屏幕旋转状态,不需开启屏幕旋转
-(void)rotation_icon:(float)n { UIButton *history_btn= [self.view viewWithTag:<#(NSInteger)#>] ...
- ios 设置屏幕方向的两种方法
第一种:通过人为的办法改变view.transform的属性. 具体办法: view.transform一般是View的旋转,拉伸移动等属性,类似view.layer.transform,区别在于Vi ...
- iOS 检测屏幕是否锁定 🔓 / 🔒
1. 导入头文件 #import <notify.h> 2. 给 CFNotificationCenter 添加观察者 - (void)addLockStatusObserver { CF ...
- Android开发之屏幕方向
一.处理屏幕方向变化的两种技术 1.锚定方法 2.调整大小和重新定位,这种方法一般是分别为横向和纵向两种模式各自定义用户界面xml界面文件,当方向变化时读取对应的界面配置文件即可. 二.检测屏幕方向改 ...
- Windows phone开发 页面布局之屏幕方向
(博客部分内容参考Windows phone开发文档) Windows phone的屏幕方向是利用Windows phone设备的方向传感器提供的数据实现切换的. Windows Phone支持纵向和 ...
- 谈谈iOS中的屏幕方向
众所周知,iOS中提供了[UIDevice currentDevice].orientation与[UIApplication sharedApplication].statusBarOrientat ...
- iOS如何用代码控制以不同屏幕方向打开新页面?
转载:http://blogread.cn/it/article/7765?f=wb#original 代码示例:https://github.com/johnlui/Swift-On-iOS/tre ...
- iOS 屏幕方向
参考文章:http://www.tuicool.com/articles/e2q6zi 一般的应用,只会支持竖屏正方向一个方向,支持多个屏幕方向的应用还是比较少的. 当时也没搞明白,所以直接就设置了正 ...
随机推荐
- 每天一个Linux命令(19)--find命令
linux 下 find 命令在目录结构中搜索文件,并执行指定的操作.Linux 下 find 命令提供了相当多的查找条件,功能很强大.由于 find 具有强大的功能,所以它的选项也很多,其中大部分选 ...
- C# .NET更智能的数据库操作的封装完整版(重构)
前述: 第一次发表文章,不过是对数据库简单的封装,主要是阐述下思路.那么在上篇文章,在大家的指导下和提出意见,并自己对代码进行了思考.在这两天我重构了新的框架,我觉得我写的可以称得上框架,为什么?请大 ...
- iOS 注册密码加密 添加了时间戳 遇到的问题...
今天项目 遇到一个事故,我本想用 一个形容这个事故的adj 算了 既然 叫事故 已经能表达我们处于的一种状态, 是这样的: 有小部分用户反应 app无法注册 总提示密码错误的情况 实际 该步骤 已经 ...
- Cocos2d-x shader学习1: 灰度shader
灰度shader 最近在学习shader,就把cocos2d-x 3.x版本中的很简单也很常用的灰度shader拿出来学习一下. #ifdef GL_ES precision mediump floa ...
- 【2017-03-09】SQL Server 数据库基础、四种约束
一.数据库和内存的区别 数据库:一些存储在硬盘上的数据文件 内存:计算机临时存储的一些数据 二.常用数据库 .Net - SQL Server PHP - MySql Java - Oreacl 三. ...
- request.RequestContextListener
由于是使用spring mvc来做项目,因此脱离了HttpServletRequest作为参数,不能够直接使用request,要想使用request可以使用下面的方法: 在web点xml中配置一个监听 ...
- Web平台安装及检测程序
软件名称:microsoft web platform installer 上图: 可以看做是一个App Store, 你再也不用东奔西跑去找什么开发软件,CMS等等了,直接打开这个,勾选上就安装吧, ...
- 强大的健身软件——Keep
Keep是一款具有社交属性的健身工具类产品.用户可利用碎片化的时间,随时随地选择适合自己的视频健身课程,进行真人同步训练.完成后还可以"打卡"晒成就. 你可根据器械.部位.难度 ...
- 如何理解jQuery中的ajaxSubmit方法
版权声明:本文为博主原创文章,转载请标注:www.cnblogs.com/gdsblog 刚刚学习中,使用到了ajaxSubmit,犹豫以前没有接触㢧这个,所以刚开始是一脸懵逼状态,最后通过查找资料的 ...
- android sdk 深入理解adb
adb 服务器-客户端程序包括3部分 1.客户端(client) 在开发机器上运行,可通过adb命令行呼叫客户端,ADT插件和DDMS同样需要adb客户端 2.服务端(server) 在开发机器上 ...