IOS屏幕旋转
1.设计窗口,添加三个按钮

2.添加事件连接
3.按钮点击事件实现
先添加三个IBAction
实现IBAction

使用旋转立刻生效(iOS16)
- -(IBAction)btnFixPortrait:(id)sender{
- //访问应用程序委托成员
- _app.mask = UIInterfaceOrientationMaskPortrait;//设置窗口旋转属性
- [self setNeedsUpdateOfSupportedInterfaceOrientations];//使用设置立刻生效
- //mask = UIInterfaceOrientationMaskPortrait;//只支持竖屏
- }
-
- -(IBAction)btnFixLand:(id)sender{
- _app.mask = UIInterfaceOrientationMaskLandscape;//设置窗口旋转属性
- [self setNeedsUpdateOfSupportedInterfaceOrientations];//使用设置立刻生效
- //mask = UIInterfaceOrientationMaskLandscape;//只支持横屏
- }
兼容方法:
-
- -(IBAction)btnFixPortrait:(id)sender{
- mask = UIInterfaceOrientationMaskPortrait;//只支持竖屏
- if (@available(iOS 16.0, *)) {
- [self setNeedsUpdateOfSupportedInterfaceOrientations];
- } else {
- //IOS 15及以下版本
- AppDelegate* dlg = [UIApplication sharedApplication].delegate;
- [dlg application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
- [[UIDevice currentDevice] setValue:@(UIDeviceOrientationPortrait) forKey:@"orientation"];
- //刷新
- [UIViewController attemptRotationToDeviceOrientation];
- }
- }
-
- -(IBAction)btnFixLand:(id)sender{
- mask = UIInterfaceOrientationMaskLandscape;//只支持横屏
- if (@available(iOS 16.0, *)) {
- [self setNeedsUpdateOfSupportedInterfaceOrientations];
- } else {
- //IOS 15及以下版本
- AppDelegate* dlg = [UIApplication sharedApplication].delegate;
- [dlg application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
- [[UIDevice currentDevice] setValue:@(UIDeviceOrientationLandscapeLeft) forKey:@"orientation"];
- //刷新
- [UIViewController attemptRotationToDeviceOrientation];
- }
- }
如果IOS版本小于15的要去掉下面这两个选项,否则无法旋转屏幕

4.监听屏幕旋转事件
- //注册屏幕旋转监听
- [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
- [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
-
- //屏幕监听事件处理
- -(void)deviceOrientationDidChange{
- NSLog(@"%lu",[self supportedInterfaceOrientations]);
- switch ([UIDevice currentDevice].orientation) {
- case UIInterfaceOrientationUnknown:
- NSLog(@"Unknown");
- break;
- case UIInterfaceOrientationPortrait:
- NSLog(@"Portrait");
- break;
- case UIInterfaceOrientationPortraitUpsideDown:
- NSLog(@"UpsideDown");
- break;
- case UIInterfaceOrientationLandscapeLeft:
- NSLog(@"LandscapeLeft");
- break;
- case UIInterfaceOrientationLandscapeRight:
- NSLog(@"LandscapeRight");
- break;
- default:
- break;
- }
- }
完整示例:
- //
- // ViewController.m
- // ios_screen_test
- //
- // Created by Hacker X on 2023/10/10.
- //
-
- #import "ViewController.h"
- #import "AppDelegate.h"
-
- @interface ViewController ()
- #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
- #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
- -(IBAction)btnGetOrientation:(id)sender;
- -(IBAction)btnFixPortrait:(id)sender;
- -(IBAction)btnFixLand:(id)sender;
- @property(nonatomic) UIInterfaceOrientationMask mask;
- @end
-
- @implementation ViewController
- @synthesize mask;
- - (void)viewDidLoad {
- [super viewDidLoad];
- [UIViewController attemptRotationToDeviceOrientation];
- mask = UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscape;//支持横竖屏切换
- //注册屏幕旋转监听
- [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
- [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
-
- [ViewController getDeviceOrientation];
- }
-
- -(IBAction)btnGetOrientation:(id)sender{
- [ViewController getDeviceOrientation];
- }
-
- -(IBAction)btnFixPortrait:(id)sender{
- mask = UIInterfaceOrientationMaskPortrait;//只支持竖屏
- if (@available(iOS 16.0, *)) {
- [self setNeedsUpdateOfSupportedInterfaceOrientations];
- } else {
- //IOS 15及以下版本
- AppDelegate* dlg = [UIApplication sharedApplication].delegate;
- [dlg application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
- [[UIDevice currentDevice] setValue:@(UIDeviceOrientationPortrait) forKey:@"orientation"];
- //刷新
- [UIViewController attemptRotationToDeviceOrientation];
- }
- }
-
- -(IBAction)btnFixLand:(id)sender{
- mask = UIInterfaceOrientationMaskLandscape;//只支持横屏
- if (@available(iOS 16.0, *)) {
- [self setNeedsUpdateOfSupportedInterfaceOrientations];
- } else {
- //IOS 15及以下版本
- AppDelegate* dlg = [UIApplication sharedApplication].delegate;
- [dlg application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
- [[UIDevice currentDevice] setValue:@(UIDeviceOrientationLandscapeLeft) forKey:@"orientation"];
- //刷新
- [UIViewController attemptRotationToDeviceOrientation];
- }
- }
- +(void)setPortrait:(UIInterfaceOrientationMask) mask{
- mask = UIInterfaceOrientationMaskPortrait;//只支持竖屏
- }
-
- +(void)setLandscape:(UIInterfaceOrientationMask) mask{
- mask = UIInterfaceOrientationMaskLandscape;//只支持横屏
- }
-
- +(UIDeviceOrientation)getDeviceOrientation{
- NSLog(@"getDeviceOrientation:%ld",[UIDevice currentDevice].orientation);
- return [UIDevice currentDevice].orientation;
- }
-
- -(void)deviceOrientationDidChange{
- switch ([UIDevice currentDevice].orientation) {
- case UIInterfaceOrientationUnknown:
- NSLog(@"Unknown");
- break;
- case UIInterfaceOrientationPortrait:
- NSLog(@"Portrait");
- break;
- case UIInterfaceOrientationPortraitUpsideDown:
- NSLog(@"UpsideDown");
- break;
- case UIInterfaceOrientationLandscapeLeft:
- NSLog(@"LandscapeLeft");
- break;
- case UIInterfaceOrientationLandscapeRight:
- NSLog(@"LandscapeRight");
- break;
- default:
- break;
- }
- }
-
- - (BOOL)prefersStatusBarHidden {
- return NO;
- }
-
- - (BOOL)shouldAutorotate{
- return YES;
- }
-
-
- - (UIInterfaceOrientationMask)supportedInterfaceOrientations{
- return mask;
- }
-
- @end
上面是针对ViewControlle的,下面是针对 窗口的
- - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
- //return UIInterfaceOrientationMaskAll;
- //return UIInterfaceOrientationMaskPortrait;
- return UIInterfaceOrientationMaskLandscape;
- //默认是这个UIInterfaceOrientationMaskAllButUpsideDown
- }