ios(ipad,iphone)屏幕旋转检测通用方法
来源:互联网 发布:抓住那个家伙知乎 编辑:程序博客网 时间:2024/06/09 19:09
转载自: http://blog.csdn.net/openglnewbee/article/details/40404495
在特别的场景下,需要针对屏幕旋转作特殊处理。在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(ipad,iphone)屏幕旋转检测通用方法
- ios(ipad,iphone)屏幕旋转检测通用方法
- ipad,iphone 屏幕旋转支持
- IOS(iphone,ipad,itouch)开发 之 屏幕旋转
- iOS:适配(一)--Universal通用程序 iPhone、iPad界面(方法)
- IOS iPhone、iPad屏幕尺寸
- iOS(iPhone/iPad) 屏幕旋转响应函数的缺点与窗口大小位置调整,以及解决办法
- iOS:界面适配(一)--Universal通用程序 iPhone、iPad适配(方法)
- iOS:界面适配(一)--Universal通用程序 iPhone、iPad适配(方法)
- iOS:界面适配(一)--Universal通用程序 iPhone、iPad适配(方法)
- IOS UIDevice & IOS检测屏幕旋转实例
- IPad/IPhone屏幕翻转
- iphone/ipad中代码下拉屏幕刷新的方法
- 检测iPhone/iPad设备方向的三种方法
- iPhone/iPad 屏幕旋屏
- iPad与iPhone屏幕布局
- Iphone屏幕旋转
- Iphone屏幕旋转
- MongoDB三(集合操作)
- 散列
- vmware安装centos
- 值得推荐的C/C++框架和库
- AI(人工智能)—人类的终结者,我们最后的发明。
- ios(ipad,iphone)屏幕旋转检测通用方法
- Java_exception_如何优雅的处理异常
- 分布式缓存Redis Centos下单节点安装
- 重装系统后win7旗舰版sp1安装oracle 10g 不能进入图形界面的解决方法
- CentOS 7:firewalld启动不了 提示 Active: failed (Result: timeout)
- 第八届ACM校赛->上色的纱雾
- embedding可视化/yale人脸数据集
- 个人小笔记
- Vue之插件使用