设备信息及方向适应

//
//  ViewController.m
//
 
#import "ViewController.h"
 
@interface ViewController ()
/**
  *  UIImageView
  */
@property(nonatomic,strong)UIImageView *imageView;
 
 
 
 
@implementation ViewController
 
 
- ( 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 ;
     }
 
}
 
 
- ( void )viewDidLoad {
 
     
     //设备名称  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(@ "UUID:%@" , identifierForVendor.UUIDString);
     
     
     
     /**
      * UIImage 对象
      */
     UIImage *image = [UIImage imageNamed:@ "scroll.jpg" ];
     self.imageView.image = image;
     
     // 设置图片范围
     CGFloat imageH = image.size.height;
     CGFloat imageW = image.size.width;
     CGFloat imageX = 0;
     CGFloat imageY = 0;
     self.imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
     [self.view addSubview:self.imageView];
     
     [super viewDidLoad];
     // Do any additional setup after loading the view, typically from a nib.
}
 
 
-( void )viewDidAppear:( BOOL )animated
{
     
     /**
      *  开始生成 设备旋转 通知
      */
     [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
     
     
     /**
      *  添加 设备旋转 通知
      *
      *  @param handleDeviceOrientationDidChange: handleDeviceOrientationDidChange: description
      *
      *  @return return value description
      */
     [[NSNotificationCenter defaultCenter] addObserver:self
                                              selector:@selector(handleDeviceOrientationDidChange:)
                                                  name:UIDeviceOrientationDidChangeNotification
                                                object:nil
      ];
     
 
 
}
 
 
 
-( void )viewDidDisappear:( BOOL )animated
{
     
     
     
     /**
      *  销毁 设备旋转 通知
      *
      *  @return return value description
      */
     [[NSNotificationCenter defaultCenter] removeObserver:self
                                                     name:UIDeviceOrientationDidChangeNotification
                                                   object:nil
      ];
     
     
     /**
      *  结束 设备旋转通知
      *
      *  @return return value description
      */
     [[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications];
     
}
 
 
 
- ( void )didReceiveMemoryWarning {
     [super didReceiveMemoryWarning];
     // Dispose of any resources that can be recreated.
}
 
 
 
 
#pragma 懒加载
 
- (UIImageView *)imageView
{
     if  (!_imageView) {
         _imageView = [[UIImageView alloc] init];
     }
     return  _imageView;
}
 
@end


转载于:https://my.oschina.net/u/2330410/blog/388076

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值