• IOS屏幕旋转监听


    IOS屏幕旋转

    1.设计窗口,添加三个按钮 

    2.添加事件连接

     3.按钮点击事件实现

    先添加三个IBAction

    实现IBAction

    使用旋转立刻生效(iOS16)

    1. -(IBAction)btnFixPortrait:(id)sender{
    2. //访问应用程序委托成员
    3. _app.mask = UIInterfaceOrientationMaskPortrait;//设置窗口旋转属性
    4. [self setNeedsUpdateOfSupportedInterfaceOrientations];//使用设置立刻生效
    5. //mask = UIInterfaceOrientationMaskPortrait;//只支持竖屏
    6. }
    7. -(IBAction)btnFixLand:(id)sender{
    8. _app.mask = UIInterfaceOrientationMaskLandscape;//设置窗口旋转属性
    9. [self setNeedsUpdateOfSupportedInterfaceOrientations];//使用设置立刻生效
    10. //mask = UIInterfaceOrientationMaskLandscape;//只支持横屏
    11. }

    兼容方法:

    1. -(IBAction)btnFixPortrait:(id)sender{
    2. mask = UIInterfaceOrientationMaskPortrait;//只支持竖屏
    3. if (@available(iOS 16.0, *)) {
    4. [self setNeedsUpdateOfSupportedInterfaceOrientations];
    5. } else {
    6. //IOS 15及以下版本
    7. AppDelegate* dlg = [UIApplication sharedApplication].delegate;
    8. [dlg application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
    9. [[UIDevice currentDevice] setValue:@(UIDeviceOrientationPortrait) forKey:@"orientation"];
    10. //刷新
    11. [UIViewController attemptRotationToDeviceOrientation];
    12. }
    13. }
    14. -(IBAction)btnFixLand:(id)sender{
    15. mask = UIInterfaceOrientationMaskLandscape;//只支持横屏
    16. if (@available(iOS 16.0, *)) {
    17. [self setNeedsUpdateOfSupportedInterfaceOrientations];
    18. } else {
    19. //IOS 15及以下版本
    20. AppDelegate* dlg = [UIApplication sharedApplication].delegate;
    21. [dlg application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
    22. [[UIDevice currentDevice] setValue:@(UIDeviceOrientationLandscapeLeft) forKey:@"orientation"];
    23. //刷新
    24. [UIViewController attemptRotationToDeviceOrientation];
    25. }
    26. }

    如果IOS版本小于15的要去掉下面这两个选项,否则无法旋转屏幕

    4.监听屏幕旋转事件

    1. //注册屏幕旋转监听
    2. [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    3. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
    4. [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
    5. //屏幕监听事件处理
    6. -(void)deviceOrientationDidChange{
    7. NSLog(@"%lu",[self supportedInterfaceOrientations]);
    8. switch ([UIDevice currentDevice].orientation) {
    9. case UIInterfaceOrientationUnknown:
    10. NSLog(@"Unknown");
    11. break;
    12. case UIInterfaceOrientationPortrait:
    13. NSLog(@"Portrait");
    14. break;
    15. case UIInterfaceOrientationPortraitUpsideDown:
    16. NSLog(@"UpsideDown");
    17. break;
    18. case UIInterfaceOrientationLandscapeLeft:
    19. NSLog(@"LandscapeLeft");
    20. break;
    21. case UIInterfaceOrientationLandscapeRight:
    22. NSLog(@"LandscapeRight");
    23. break;
    24. default:
    25. break;
    26. }
    27. }

     完整示例:

    1. //
    2. // ViewController.m
    3. // ios_screen_test
    4. //
    5. // Created by Hacker X on 2023/10/10.
    6. //
    7. #import "ViewController.h"
    8. #import "AppDelegate.h"
    9. @interface ViewController ()
    10. #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
    11. #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
    12. -(IBAction)btnGetOrientation:(id)sender;
    13. -(IBAction)btnFixPortrait:(id)sender;
    14. -(IBAction)btnFixLand:(id)sender;
    15. @property(nonatomic) UIInterfaceOrientationMask mask;
    16. @end
    17. @implementation ViewController
    18. @synthesize mask;
    19. - (void)viewDidLoad {
    20. [super viewDidLoad];
    21. [UIViewController attemptRotationToDeviceOrientation];
    22. mask = UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscape;//支持横竖屏切换
    23. //注册屏幕旋转监听
    24. [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    25. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
    26. [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
    27. [ViewController getDeviceOrientation];
    28. }
    29. -(IBAction)btnGetOrientation:(id)sender{
    30. [ViewController getDeviceOrientation];
    31. }
    32. -(IBAction)btnFixPortrait:(id)sender{
    33. mask = UIInterfaceOrientationMaskPortrait;//只支持竖屏
    34. if (@available(iOS 16.0, *)) {
    35. [self setNeedsUpdateOfSupportedInterfaceOrientations];
    36. } else {
    37. //IOS 15及以下版本
    38. AppDelegate* dlg = [UIApplication sharedApplication].delegate;
    39. [dlg application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
    40. [[UIDevice currentDevice] setValue:@(UIDeviceOrientationPortrait) forKey:@"orientation"];
    41. //刷新
    42. [UIViewController attemptRotationToDeviceOrientation];
    43. }
    44. }
    45. -(IBAction)btnFixLand:(id)sender{
    46. mask = UIInterfaceOrientationMaskLandscape;//只支持横屏
    47. if (@available(iOS 16.0, *)) {
    48. [self setNeedsUpdateOfSupportedInterfaceOrientations];
    49. } else {
    50. //IOS 15及以下版本
    51. AppDelegate* dlg = [UIApplication sharedApplication].delegate;
    52. [dlg application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
    53. [[UIDevice currentDevice] setValue:@(UIDeviceOrientationLandscapeLeft) forKey:@"orientation"];
    54. //刷新
    55. [UIViewController attemptRotationToDeviceOrientation];
    56. }
    57. }
    58. +(void)setPortrait:(UIInterfaceOrientationMask) mask{
    59. mask = UIInterfaceOrientationMaskPortrait;//只支持竖屏
    60. }
    61. +(void)setLandscape:(UIInterfaceOrientationMask) mask{
    62. mask = UIInterfaceOrientationMaskLandscape;//只支持横屏
    63. }
    64. +(UIDeviceOrientation)getDeviceOrientation{
    65. NSLog(@"getDeviceOrientation:%ld",[UIDevice currentDevice].orientation);
    66. return [UIDevice currentDevice].orientation;
    67. }
    68. -(void)deviceOrientationDidChange{
    69. switch ([UIDevice currentDevice].orientation) {
    70. case UIInterfaceOrientationUnknown:
    71. NSLog(@"Unknown");
    72. break;
    73. case UIInterfaceOrientationPortrait:
    74. NSLog(@"Portrait");
    75. break;
    76. case UIInterfaceOrientationPortraitUpsideDown:
    77. NSLog(@"UpsideDown");
    78. break;
    79. case UIInterfaceOrientationLandscapeLeft:
    80. NSLog(@"LandscapeLeft");
    81. break;
    82. case UIInterfaceOrientationLandscapeRight:
    83. NSLog(@"LandscapeRight");
    84. break;
    85. default:
    86. break;
    87. }
    88. }
    89. - (BOOL)prefersStatusBarHidden {
    90. return NO;
    91. }
    92. - (BOOL)shouldAutorotate{
    93. return YES;
    94. }
    95. - (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    96. return mask;
    97. }
    98. @end

    上面是针对ViewControlle的,下面是针对 窗口的

    1. - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    2. //return UIInterfaceOrientationMaskAll;
    3. //return UIInterfaceOrientationMaskPortrait;
    4. return UIInterfaceOrientationMaskLandscape;
    5. //默认是这个UIInterfaceOrientationMaskAllButUpsideDown
    6. }

  • 相关阅读:
    MySQL系统表information_schema.INNODB_TRX详解及查看当前运行事务
    SpringCloud Sleuth分布式请求链路跟踪
    工作卷?一行更比一行卷
    面试题:经典常见排序算法 插入 冒泡 选择 归并 快速排序
    沟通管理&风险管理&采购管理@&相关方管理
    Hive企业级调优
    Gartner 存储与数据保护技术 Hype Cycle 解读|SmartX 趋势分享
    如何发现新的潜力项目?工具推荐
    DRDS的介绍
    ubuntu20.04 实测 机械式激光雷达与相机联合标定
  • 原文地址:https://blog.csdn.net/fittec/article/details/133967440