設備信息及方向適應

//
//  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
相關文章
相關標籤/搜索