美文网首页
iOS 手机屏幕旋转

iOS 手机屏幕旋转

作者: 双门 | 来源:发表于2023-08-13 17:14 被阅读0次
    -(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;
        }
    }
    
    

    相关文章

      网友评论

          本文标题:iOS 手机屏幕旋转

          本文链接:https://www.haomeiwen.com/subject/fjxqmdtx.html