/**
* 开始生成 设备旋转 通知
*/
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
/**
* 添加 设备旋转 通知
*
* 当监听到 UIDeviceOrientationDidChangeNotification 通知时,调用handleDeviceOrientationDidChange:方法
* @param handleDeviceOrientationDidChange: handleDeviceOrientationDidChange: description
*
* @return return value description
*/
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleDeviceOrientationDidChange:)
name:UIDeviceOrientationDidChangeNotification
object:nil
];
/**
* 销毁 设备旋转 通知
*
* @return return value description
*/
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIDeviceOrientationDidChangeNotification
object:nil
];
/**
* 结束 设备旋转通知
*
* @return return value description
*/
[[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications];
- (void)handleDeviceOrientationDidChange:(UIInterfaceOrientation)interfaceOrientation
{
//1.获取 当前设备 实例
UIDevice *device = [UIDevice currentDevice] ;
/**
* 2.取得当前Device的方向,Device的方向类型为Integer
*
* 必须调用beginGeneratingDeviceOrientationNotifications方法后,此orientation属性才有效,否则一直是0。orientation用于判断设备的朝向,与应用UI方向无关
*
* @param device.orientation
*
*/
switch (device.orientation) {
case UIDeviceOrientationFaceUp:
NSLog(@"屏幕朝上平躺");
break;
case UIDeviceOrientationFaceDown:
NSLog(@"屏幕朝下平躺");
break;
//系統無法判斷目前Device的方向,有可能是斜置
case UIDeviceOrientationUnknown:
NSLog(@"未知方向");
break;
case UIDeviceOrientationLandscapeLeft:
NSLog(@"屏幕向左横置");
break;
case UIDeviceOrientationLandscapeRight:
NSLog(@"屏幕向右橫置");
break;
case UIDeviceOrientationPortrait:
NSLog(@"屏幕直立");
break;
case UIDeviceOrientationPortraitUpsideDown:
NSLog(@"屏幕直立,上下顛倒");
break;
default:
NSLog(@"无法辨识");
break;
}
}
iOS 监听横屏竖屏
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 1、修改App-info.plist(在XCode中General中设置 一样的效果) 2、AppDelegate...
- 今天项目中遇到正在看视频的时候账号被挤,如果当时是横屏的情况下,需要强制竖屏。真头疼,网上找了好多方法,终于解决啦...
- 青春,总有最美丽的意外,那就是我在最好的时光里遇见了珍贵的你。青春,又有最浪漫的邂逅,那就是我在懵懂的年纪遇见同样...