iOS 手机屏幕旋转

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    //开始生成 设备旋转 通知
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

    //添加 设备旋转 通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:)  name:UIDeviceOrientationDidChangeNotification object:nil];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    // 销毁 设备旋转 通知
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIDeviceOrientationDidChangeNotification
                                                  object:nil ];
    // 结束 设备旋转通知
    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
}

/**屏幕旋转的通知回调*/
- (void)orientChange:(NSNotification *)noti {
    UIDeviceOrientation  orient = [UIDevice currentDevice].orientation;
    switch (orient) {
        case UIDeviceOrientationPortrait:
            NSLog(@"竖直屏幕");
            break;
        case UIDeviceOrientationLandscapeLeft:
            NSLog(@"手机左转");
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            NSLog(@"手机竖直");
            break;
        case UIDeviceOrientationLandscapeRight:
            NSLog(@"手机右转");
            break;
        case UIDeviceOrientationUnknown:
            NSLog(@"未知");
            break;
        case UIDeviceOrientationFaceUp:
            NSLog(@"手机屏幕朝上");
            break;
        case UIDeviceOrientationFaceDown:
            NSLog(@"手机屏幕朝下");
            break;
        default:
            break;
    }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容