美文网首页
iOS-UIDevice的简单使用

iOS-UIDevice的简单使用

作者: yuyangkk | 来源:发表于2017-07-06 13:28 被阅读1629次

1.获取设备方向

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    UIDeviceOrientationUnknown,
    UIDeviceOrientationPortrait,            // 设备垂直竖向方向放置, 并且home键在下方
    UIDeviceOrientationPortraitUpsideDown,  //设备垂直竖向方向放置, 并且home键在上方
    UIDeviceOrientationLandscapeLeft,       // 设备垂直横向方向放置, 并且home键在右方
    UIDeviceOrientationLandscapeRight,      // 设备垂直横向方向放置, 并且home键在左方
    UIDeviceOrientationFaceUp,              // 设备水平放置, 屏幕在上方
    UIDeviceOrientationFaceDown             // 设备水平放置, 屏幕在下方
} (TVOS不可用)

判断当前设备方向

//但这个有个弊端,就是没有设备方向改变通知时,返回UIDeviceOrientationUnknown
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait){
    NSLog(@"当前设备是垂直竖向,并且home键在下方");
}
//竖屏,包括UIDeviceOrientationPortrait || UIDeviceOrientationPortraitUpsideDown
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)) {
    NSLog(@"竖屏");
}
//横屏,包括UIDeviceOrientationLandscapeLeft || UIDeviceOrientationLandscapeRight
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
    NSLog(@"横屏");
}

//所以推荐下面的方式
if (![UIDevice currentDevice].generatesDeviceOrientationNotifications) {
   [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
 }
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait){
    NSLog(@"当前设备是垂直竖向,并且home键在下方");
}
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

2.获取设备电池状态

typedef NS_ENUM(NSInteger, UIDeviceBatteryState) {
    UIDeviceBatteryStateUnknown,
    UIDeviceBatteryStateUnplugged,   // 未充电,使用电池
    UIDeviceBatteryStateCharging,    // 充电中,并且电量小于 100%
    UIDeviceBatteryStateFull,        // 充电中, 电量已达 100%
} __TVOS_PROHIBITED;              // TVOS不可用,iOS3之后可用

判断当前设备电池状态

[UIDevice currentDevice].batteryMonitoringEnabled = YES; //开启电量监控,默认不开启
if ([[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateCharging){
    NSLog(@"正在充电中,且电量不足100");
}
//当前电量,小数[0,1] ,如果[[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateUnknown时,则为1.0
NSLog(@"当前电量%f",[[UIDevice currentDevice] batteryLevel]);

3.获取设备类型

typedef NS_ENUM(NSInteger, UIUserInterfaceIdiom) {
    UIUserInterfaceIdiomUnspecified = -1,
    UIUserInterfaceIdiomPhone NS_ENUM_AVAILABLE_IOS(3_2), // 0,iPhone and iPod touch style UI
    UIUserInterfaceIdiomPad NS_ENUM_AVAILABLE_IOS(3_2), // 1,iPad style UI
    UIUserInterfaceIdiomTV NS_ENUM_AVAILABLE_IOS(9_0), // 2,Apple TV style UI
    UIUserInterfaceIdiomCarPlay NS_ENUM_AVAILABLE_IOS(9_0), //3, CarPlay style UI
};
NSLog(@"%ld",(long)[UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) //判断是否为iPhone或者iPod

4.修改某个界面的横竖屏状态

上述方法只能获取设备的当前屏幕状态,不能修改,如果想要修改,请参考下面的方法
iOS6之前

// Applications should use supportedInterfaceOrientations and/or shouldAutorotate..
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0) __TVOS_PROHIBITED;

示例

//始终保持竖屏
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

iOS6之后

//是否支持旋转
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;
//支持的旋转方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;
  • 全部界面采用同一种
    如果window.rootViewController是UINavigationViewController的话,只要在导航控制器中设置完毕之后,所有push出来的viewController,都是和NavigationViewController一样
- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}
  • 多个界面,采用各自的屏幕需求,在NavigationViewController中实现下面的方法,指定每个ViewController按照自己的需求改变,在ViewController中覆写此方法
- (BOOL)shouldAutorotate {
    if (self.topViewController != nil) return [self.topViewController shouldAutorotate];
    else return [super shouldAutorotate];
}

- (NSUInteger)supportedInterfaceOrientations {
    if (self.topViewController != nil) return [self.topViewController supportedInterfaceOrientations];
    else return [super supportedInterfaceOrientations];
}

相关文章

  • iOS-UIDevice的简单使用

    1.获取设备方向 判断当前设备方向 2.获取设备电池状态 判断当前设备电池状态 3.获取设备类型 4.修改某个界面...

  • 简单使用

    创建模型 过滤器 我们有一些字段和我们想让用户筛选的基础上 名称、价格或release_date。 我们创建一个 ...

  • gorange

    数组中简单使用 map中简单使用

  • UICollectionViewLayout的简单使用(简单瀑布

    对于需要使用到列表的页面,一般是使用UITableView或者是UICollectionView来实现。一直以来都...

  • 零碎的小程序笔记

    目录 template的简单使用WXS的简单使用npm的简单使用倒计时js的实现wx:for的使用一些js方法记录...

  • 简单使用使用kaggle

    向我这样的条件不好的可以考虑借助云gpu来加速训练,借助kaggle可以在kaggle服务器上训练数据,kaggl...

  • 命令行的简单使用

    Git命令行的简单使用,仅供自己使用 pod命令行的简单使用

  • Alamofire类似AFNetworking的简单使用和封装

    简单的使用。简单的使用。简单的使用。注定该文弱鸡一个,求拍砖。 一、介绍 Alamofire(Swift)的前身是...

  • shiro的简单使用

    大家好,我是IT修真院北京分院第26期的学员,一枚正直纯洁善良的JAVA程序员 今天给大家分享一下,修真院官网JA...

  • RAC的简单使用

    新项目今天提测,项目中用到了RAC&MVVM框架,简单记录下RAC的简单使用 项目是OC开发,用的是Reactiv...

网友评论

      本文标题:iOS-UIDevice的简单使用

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