美文网首页
【转】IOS UIDevice & IOS检测屏幕旋转实例

【转】IOS UIDevice & IOS检测屏幕旋转实例

作者: 棒棒德 | 来源:发表于2019-12-30 18:29 被阅读0次

一 UIDevice 简介

UIDevice类提供了一个单例实例代表当前的设备。从这个实例中可以获得的信息设备,比如操作系统名称、电池电量值(batteryLevel)、电池状态(batteryState)、设备的类型(model,比如iPod、iPhone等)、设备的系统(systemVersion)

二 获取 UIDevice 实例

通过[UIDevice currentDevice]可以获取这个单粒对象

UIDevice *device = [UIDevice currentDevice];

三 UIDevice 常用属性

通过UIDevice相关属性,可用获取设备信息

//设备名称  e.g. "My iPhone"NSString*strName=[[UIDevice currentDevice]name];NSLog(@"设备名称:%@",strName);/**

    * 系统名称 e.g. @"iOS"

    */NSString*strSysName=[[UIDevice currentDevice]systemName];NSLog(@"系统名称:%@",strSysName);/**

    * 系统版本号 e.g. @"4.0"

    */NSString*strSysVersion=[[UIDevice currentDevice]systemVersion];NSLog(@"系统版本号:%@",strSysVersion);/**

    * 设备类型 e.g. @"iPhone", @"iPod touch"

    */NSString*strModel=[[UIDevice currentDevice]model];NSLog(@"设备类型:%@",strModel);/**

    * 本地设备模式 localized version of model

    */NSString*strLocModel=[[UIDevice currentDevice]localizedModel];NSLog(@"本地设备模式:%@",strLocModel);/**

    * UUID  可用于唯一地标识该设备

    */NSUUID*identifierForVendor=[[UIDevice currentDevice]identifierForVendor];NSLog(@"strIdentifierForVendor:%@",identifierForVendor.UUIDString);

四 UIDevice 通知

UIDevice对象会不间断地发布一些通知,下列是UIDevice对象所发布通知的名称常量:

4.1 设备旋转UIDeviceOrientationDidChangeNotification4.2 电池状态改变UIDeviceBatteryStateDidChangeNotification4.3 电池电量改变UIDeviceBatteryLevelDidChangeNotification4.4 近距离传感器(比如设备贴近了使用者的脸部)UIDeviceProximityStateDidChangeNotification

五 IOS检测屏幕旋转 UIDeviceOrientationDidChangeNotification 使用举例(其他通知方式类推)

屏幕的旋转朝向可以通过  [[UIDevice currentDevice]orientation] 判断,orientation是个Integer类型,每个值表示相应的朝向,必须在调用beginGeneratingDeviceOrientationNotifications方法后,此orientation属性才有效,否则一直是0。

orientation 对应的枚举值typedefNS_ENUM(NSInteger,UIDeviceOrientation){UIDeviceOrientationUnknown,UIDeviceOrientationPortrait,// Device oriented vertically, home button on the bottomUIDeviceOrientationPortraitUpsideDown,// Device oriented vertically, home button on the topUIDeviceOrientationLandscapeLeft,// Device oriented horizontally, home button on the rightUIDeviceOrientationLandscapeRight,// Device oriented horizontally, home button on the leftUIDeviceOrientationFaceUp,// Device oriented flat, face upUIDeviceOrientationFaceDown// Device oriented flat, face down};5.1注册通知/**

    *  开始生成 设备旋转 通知

    */[[UIDevice currentDevice]beginGeneratingDeviceOrientationNotifications];/**

    *  添加 设备旋转 通知

    * 

    *  当监听到 UIDeviceOrientationDidChangeNotification 通知时,调用handleDeviceOrientationDidChange:方法

    *  @param handleDeviceOrientationDidChange: handleDeviceOrientationDidChange: description

    *

    *  @return return value description

    */[[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(handleDeviceOrientationDidChange:)name:UIDeviceOrientationDidChangeNotification                                              object:nil];5.2销毁通知/**

    *  销毁 设备旋转 通知

    *

    *  @return return value description

    */[[NSNotificationCenter defaultCenter]removeObserver:selfname:UIDeviceOrientationDidChangeNotification                                                  object:nil];/**

    *  结束 设备旋转通知

    *

    *  @return return value description

    */[[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications];5.3旋转识别-(void)handleDeviceOrientationDidChange:(UIInterfaceOrientation)interfaceOrientation{//1.获取 当前设备 实例UIDevice*device=[UIDevice currentDevice];/**

    *  2.取得当前Device的方向,Device的方向类型为Integer

    *

    *  必须调用beginGeneratingDeviceOrientationNotifications方法后,此orientation属性才有效,否则一直是0。orientation用于判断设备的朝向,与应用UI方向无关

    *

    *  @param device.orientation

    *

    */switch(device.orientation){caseUIDeviceOrientationFaceUp:NSLog(@"屏幕朝上平躺");break;caseUIDeviceOrientationFaceDown:NSLog(@"屏幕朝下平躺");break;//系統無法判斷目前Device的方向,有可能是斜置caseUIDeviceOrientationUnknown:NSLog(@"未知方向");break;caseUIDeviceOrientationLandscapeLeft:NSLog(@"屏幕向左横置");break;caseUIDeviceOrientationLandscapeRight:NSLog(@"屏幕向右橫置");break;caseUIDeviceOrientationPortrait:NSLog(@"屏幕直立");break;caseUIDeviceOrientationPortraitUpsideDown:NSLog(@"屏幕直立,上下顛倒");break;default:NSLog(@"无法辨识");break;}}

链接:https://www.jianshu.com/p/973eff69d2de

相关文章

  • 【转】IOS UIDevice & IOS检测屏幕旋转实例

    一 UIDevice 简介 UIDevice类提供了一个单例实例代表当前的设备。从这个实例中可以获得的信息设备,比...

  • IOS UIDevice & IOS检测屏幕旋转实例

    项目需要横竖屏监测,而本身我又在设置里面禁止了横竖屏,第一次做这个,去网上查了好久,有的方法没有作用。最后看到一个...

  • iOS UIDevice & 屏幕旋转检测

    原文地址:https://my.oschina.net/wolx/blog/387315

  • iOS16 屏幕旋转

    iOS16 强行让屏幕旋转 UIDevice.current.setValue(UIInterfaceOrient...

  • iOS16屏幕旋转

    iOS16出来一段时间了,个别app出现屏幕不能强制旋转全屏,原因就是iOS不再支持UIDevice 方式的旋转下...

  • 屏幕旋转

    屏幕旋转 推荐文档 了解UIWindow——UIWindow实践 iOS屏幕旋转问题总结 IOS:屏幕旋转与变换 ...

  • 怎么样适配ios系统和ios屏幕 ios适配系统代码写法[[UIDevice currentDevice] sys...

  • OC - 屏幕旋转(自动旋转、手动旋转、兼容iOS6之前系统)

    导读: 一、iOS6之前屏幕旋转知识了解 二、iOS6(包括iOS6)之后屏幕旋转知识了解 三、自动旋转具体操作 ...

  • UIDevice

    记录UIDevice的属性和方法 其中,下面这个监听物理方向改变的通知有一个坑 可以参考iOS开发中屏幕旋转的那些坑

  • 屏幕旋转和弹出框

    iOS中控制屏幕旋转相关方法 shouldAutorotate:是否支持屏幕旋转 alertView:clicke...

网友评论

      本文标题:【转】IOS UIDevice & IOS检测屏幕旋转实例

      本文链接:https://www.haomeiwen.com/subject/gkgmoctx.html