IOS屏幕旋转监听

IOS屏幕旋转

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

2.添加事件连接

 3.按钮点击事件实现

先添加三个IBAction

IOS屏幕旋转监听_第1张图片实现IBAction

IOS屏幕旋转监听_第2张图片

使用旋转立刻生效

-(IBAction)btnFixPortrait:(id)sender{
    //访问应用程序委托成员
    _app.mask = UIInterfaceOrientationMaskPortrait;//设置窗口旋转属性
    [self setNeedsUpdateOfSupportedInterfaceOrientations];//使用设置立刻生效
    //mask = UIInterfaceOrientationMaskPortrait;//只支持竖屏
}

-(IBAction)btnFixLand:(id)sender{
    _app.mask = UIInterfaceOrientationMaskLandscape;//设置窗口旋转属性
    [self setNeedsUpdateOfSupportedInterfaceOrientations];//使用设置立刻生效
    //mask = UIInterfaceOrientationMaskLandscape;//只支持横屏
}

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"

@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];
    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;//只支持竖屏
}

-(IBAction)btnFixLand:(id)sender{
    mask = UIInterfaceOrientationMaskLandscape;//只支持横屏
}

+(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{
    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;
    }
}

- (BOOL)prefersStatusBarHidden {
    return NO;
}

//设置自动旋转
- (BOOL)shouldAutorotate{
    return NO;
}

//重写基类supportedInterfaceOrientations来设置支持方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return mask;
}

@end

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

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    //return UIInterfaceOrientationMaskAll;
    //return UIInterfaceOrientationMaskPortrait;
    return UIInterfaceOrientationMaskLandscape;
    //默认是这个UIInterfaceOrientationMaskAllButUpsideDown
}

你可能感兴趣的:(Objective-C,ios,cocoa,macos)