IOS UIDevice & IOS檢測屏幕旋轉(zhuǎn)實例

項目需要橫豎屏監(jiān)測,而本身我又在設(shè)置里面禁止了橫豎屏,第一次做這個,去網(wǎng)上查了好久,有的方法沒有作用。最后看到一個很好的文章,很有用,轉(zhuǎn)過來。

一 UIDevice 簡介

UIDevice類提供了一個單例實例代表當(dāng)前的設(shè)備。從這個實例中可以獲得的信息設(shè)備,比如操作系統(tǒng)名稱、電池電量值(batteryLevel)、電池狀態(tài)(batteryState)、設(shè)備的類型(model,比如iPod、iPhone等)、設(shè)備的系統(tǒng)(systemVersion)

二 獲取 UIDevice 實例

通過[UIDevice currentDevice]可以獲取這個單粒對象

UIDevice *device = [UIDevice currentDevice];

三 UIDevice 常用屬性

通過UIDevice相關(guān)屬性,可用獲取設(shè)備信息

    //設(shè)備名稱  e.g. "My iPhone"
    NSString *strName = [[UIDevice currentDevice] name];
    NSLog(@"設(shè)備名稱:%@", strName);

    
    
    /**
     * 系統(tǒng)名稱 e.g. @"iOS"
     */
    NSString *strSysName = [[UIDevice currentDevice] systemName];
    NSLog(@"系統(tǒng)名稱:%@", strSysName);
    
    

    /**
     * 系統(tǒng)版本號 e.g. @"4.0"
     */
    NSString *strSysVersion = [[UIDevice currentDevice] systemVersion];
    NSLog(@"系統(tǒng)版本號:%@", strSysVersion);
    
    
    
    /**
     * 設(shè)備類型 e.g. @"iPhone", @"iPod touch"
     */
    NSString *strModel = [[UIDevice currentDevice] model];
    NSLog(@"設(shè)備類型:%@", strModel);
    
    
    
    /**
     * 本地設(shè)備模式 localized version of model
     */
    NSString *strLocModel = [[UIDevice currentDevice] localizedModel];
    NSLog(@"本地設(shè)備模式:%@", strLocModel);
    

    
    /**
     * UUID  可用于唯一地標(biāo)識該設(shè)備
     */
    NSUUID *identifierForVendor = [[UIDevice currentDevice] identifierForVendor];
    NSLog(@"strIdentifierForVendor:%@", identifierForVendor.UUIDString);

四 UIDevice 通知

UIDevice對象會不間斷地發(fā)布一些通知,下列是UIDevice對象所發(fā)布通知的名稱常量:


4.1 設(shè)備旋轉(zhuǎn)

UIDeviceOrientationDidChangeNotification
4.2 電池狀態(tài)改變

UIDeviceBatteryStateDidChangeNotification
4.3 電池電量改變

UIDeviceBatteryLevelDidChangeNotification
4.4 近距離傳感器(比如設(shè)備貼近了使用者的臉部)

UIDeviceProximityStateDidChangeNotification

五 IOS檢測屏幕旋轉(zhuǎn) UIDeviceOrientationDidChangeNotification 使用舉例(其他通知方式類推)

屏幕的旋轉(zhuǎn)朝向可以通過 [[UIDevice currentDevice]orientation] 判斷,orientation是個Integer類型,每個值表示相應(yīng)的朝向,必須在調(diào)用beginGeneratingDeviceOrientationNotifications方法后,此orientation屬性才有效,否則一直是0。

   orientation 對應(yīng)的枚舉值

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    UIDeviceOrientationUnknown,
    UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom
    UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top
    UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right
    UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left
    UIDeviceOrientationFaceUp,              // Device oriented flat, face up
    UIDeviceOrientationFaceDown             // Device oriented flat, face down
};


5.1 注冊通知

    /**
     *  開始生成 設(shè)備旋轉(zhuǎn) 通知
     */
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    
    
    /**
     *  添加 設(shè)備旋轉(zhuǎn) 通知
     *  
     *  當(dāng)監(jiān)聽到 UIDeviceOrientationDidChangeNotification 通知時,調(diào)用handleDeviceOrientationDidChange:方法
     *  @param handleDeviceOrientationDidChange: handleDeviceOrientationDidChange: description
     *
     *  @return return value description
     */
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleDeviceOrientationDidChange:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil
     ];


5.2 銷毀通知

  
    /**
     *  銷毀 設(shè)備旋轉(zhuǎn) 通知
     *
     *  @return return value description
     */
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIDeviceOrientationDidChangeNotification
                                                  object:nil
     ];
    
    
    /**
     *  結(jié)束 設(shè)備旋轉(zhuǎn)通知
     *
     *  @return return value description
     */
    [[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications];


5.3 旋轉(zhuǎn)識別

- (void)handleDeviceOrientationDidChange:(UIInterfaceOrientation)interfaceOrientation
{
    //1.獲取 當(dāng)前設(shè)備 實例
    UIDevice *device = [UIDevice currentDevice] ;
    
    
    
    
    /**
     *  2.取得當(dāng)前Device的方向,Device的方向類型為Integer
     *
     *  必須調(diào)用beginGeneratingDeviceOrientationNotifications方法后,此orientation屬性才有效,否則一直是0。orientation用于判斷設(shè)備的朝向,與應(yīng)用UI方向無關(guān)
     *
     *  @param device.orientation
     *
     */

    switch (device.orientation) {
        case UIDeviceOrientationFaceUp:
            NSLog(@"屏幕朝上平躺");
            break;
            
        case UIDeviceOrientationFaceDown:
            NSLog(@"屏幕朝下平躺");
            break;
            
            //系統(tǒng)無法判斷目前Device的方向,有可能是斜置
        case UIDeviceOrientationUnknown:
            NSLog(@"未知方向");
            break;
            
        case UIDeviceOrientationLandscapeLeft:
            NSLog(@"屏幕向左橫置");
            break;
            
        case UIDeviceOrientationLandscapeRight:
            NSLog(@"屏幕向右橫置");
            break;
            
        case UIDeviceOrientationPortrait:
            NSLog(@"屏幕直立");
            break;
            
        case UIDeviceOrientationPortraitUpsideDown:
            NSLog(@"屏幕直立,上下顛倒");
            break;
            
        default:
            NSLog(@"無法辨識");
            break;
    }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容