iOS 監聽橫屏豎屏

/**
     *  開始生成 設備旋轉 通知
     */
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];


    /**
     *  添加 設備旋轉 通知
     *  
     *  當監聽到 UIDeviceOrientationDidChangeNotification 通知時,調用handleDeviceOrientationDidChange:方法
     *  @param handleDeviceOrientationDidChange: handleDeviceOrientationDidChange: description
     *
     *  @return return value description
     */
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleDeviceOrientationDidChange:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil
     ];


/**
     *  銷毀 設備旋轉 通知
     *
     *  @return return value description
     */
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIDeviceOrientationDidChangeNotification
                                                  object:nil
     ];


    /**
     *  結束 設備旋轉通知
     *
     *  @return return value description
     */
    [[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications];


- (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;
    }

}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容