iOS-UIDevice的简单使用

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];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 大三的生活太无聊,我做了一份补课班老师的兼职工作,我带的六年级的班级,有两个小学生被其他学生称之为“情侣”,他们很...
    荔枝阿姨阅读 720评论 0 3
  • | 路人 | 二菟 你对我微笑 我以微笑回你 你对我敌视 我亦以微笑回你 我们生来就是路人 可以一见如故...
    二菟阅读 112评论 0 0