IOS屏幕旋转监听

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
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林鸿群

有你的鼓励,我会更加努力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值