iOS 手机屏幕旋转

2023-08-13  本文已影响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;
    }
}

上一篇下一篇

猜你喜欢

热点阅读