转屏

作者: 冰点雨 | 来源:发表于2023-02-02 10:02 被阅读0次

添加观察者

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];


#pragma mark ––––––––––––––––– 监测横竖屏切换
#pragma mark ––––––––––––––––– 监测横竖屏切换
- (void)deviceOrientationDidChange
{
    NSLog(@"deviceOrientationDidChange:%ld",(long)[UIDevice currentDevice].orientation);
    //1.获取 当前设备 实例
        UIDevice *device = [UIDevice currentDevice];
        /**
         device.orientation
         UIDeviceOrientationUnknown,             //系統無法判斷目前Device的方向,有可能是斜置
         UIDeviceOrientationPortrait,            // 屏幕直立,home键朝下
         UIDeviceOrientationPortraitUpsideDown,  // 屏幕直立,上下顛倒,home键在上
         UIDeviceOrientationLandscapeLeft,       // 屏幕向左横置,home键在右
         UIDeviceOrientationLandscapeRight,      // 屏幕向右横置,home键在左
         UIDeviceOrientationFaceUp,              // 屏幕朝上平躺
         UIDeviceOrientationFaceDown             // 屏幕朝下平躺
         */
//    NSLog(@"deviceOrientationDidChange============ %ld",(long)[UIDevice currentDevice].orientation);
    if(device.orientation == UIDeviceOrientationUnknown || device.orientation == UIDeviceOrientationPortrait){//竖屏
        [self updateVerticalSubviews];//更新竖屏UI
    }else if(device.orientation == UIDeviceOrientationFaceUp || device.orientation == UIDeviceOrientationFaceDown){//屏幕朝上-屏幕朝下

    }else{//横屏
        [self updateHorizontalSubviews];//更新横屏样式
    }
}

移除观察者

        [[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications];
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                           name:UIDeviceOrientationDidChangeNotification
                                                      object:nil];

改变方向

- (void)setDeviceOrientation:(BOOL)isHorizontal{
    /**
     typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
         UIInterfaceOrientationMaskPortrait // 设备(屏幕)直立
         UIInterfaceOrientationMaskLandscapeLeft 屏幕向左横置
         UIInterfaceOrientationMaskLandscapeRight 屏幕向右橫置
         UIInterfaceOrientationMaskPortraitUpsideDown 未知
         UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
         UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
         UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
     }
     */
    if(isHorizontal){//横屏
        if (@available(iOS 16.0, *)) {
            [[[XXYCustomUtil sharedInstance] getCurrentVC] setNeedsUpdateOfSupportedInterfaceOrientations];
            NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
            UIWindowScene *ws = (UIWindowScene *)array[0];
            UIWindowSceneGeometryPreferencesIOS *geometryPreferences =     [[UIWindowSceneGeometryPreferencesIOS alloc] init];
            geometryPreferences.interfaceOrientations =     UIInterfaceOrientationMaskLandscapeRight;
            [self updateHorizontalSubviews];
            [ws requestGeometryUpdateWithPreferences:geometryPreferences     errorHandler:^(NSError * _Nonnull error) {
                 //业务代码
            }];
        } else {
            [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];
        }

    }else{//竖屏
        if (@available(iOS 16.0, *)) {
            [[[XXYCustomUtil sharedInstance] getCurrentVC] setNeedsUpdateOfSupportedInterfaceOrientations];
            NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
            UIWindowScene *ws = (UIWindowScene *)array[0];
            UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] init];
            geometryPreferences.interfaceOrientations = UIInterfaceOrientationMaskPortrait;
            [self updateVerticalSubviews];
            [ws requestGeometryUpdateWithPreferences:geometryPreferences errorHandler:^(NSError * _Nonnull error) {
                 //业务代码
            }];
        } else {
            [[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIInterfaceOrientationPortrait] forKey:@"orientation"];
        }
    }
}

相关文章

  • 关于移动端H5横竖屏问题

    根据项目的一些需求,经常需要横屏展示H5,但对于开启自动横竖屏的手机,横屏时转竖屏,或者竖屏转横屏时,页面布局可能...

  • 横屏竖屏切换

    项目只有竖屏的时侯,在某个页面强制横屏模态的形式推入: 在此页面进行强制转屏 转屏页面 push:推入此类继承UI...

  • iOS-视频全屏逻辑

    需求:视频小窗全屏切换 效果1:状态栏不转屏 (大多数App使用)效果2:状态栏转屏 实现: 一、状态栏不转屏 试...

  • 视频播放支持横屏

    应用整体只支持竖屏,只有特定的某个界面支持横屏 解决方法: 1.在项目中plist文件中设置支持转屏方向 转屏控制...

  • 颜色转图片、View转图片

    1、颜色转图片 2、View转图片(截屏)

  • ios手机屏幕旋转控制

    //全局控制禁止转屏

  • Ios 视频播放器转屏问题讨论

    转屏主要分为两种方式来实现: 第一种是物理转屏(强制转屏),这种方式的优点是子视图无需再次更新位置布局,能跟着父视...

  • 自动转屏

    设备方向发生变化时,应用会收到信的方向信息,并根据相关设置决定是否感觉新的设备方向调整界面方向。 如果需要修改默认...

  • [转]Android转屏流程与优化(Google转屏算法)

    和你一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文...

  • Android旋转屏封装处理

    记录一个播放视频页面的旋转屏处理封装类. 满足了我在项目中的需求: 在设置转屏开关开启的时候自动转屏,用户可点击横...

网友评论

      本文标题:转屏

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