IOS屏幕旋转监听
  Feeln6qFyXuE 2023年11月13日 36 0


IOS屏幕旋转


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

IOS屏幕旋转监听_macos

2.添加事件连接

IOS屏幕旋转监听_cocoa_02

 3.按钮点击事件实现

先添加三个IBAction

IOS屏幕旋转监听_cocoa_03

实现IBAction

IOS屏幕旋转监听_ide_04

使用旋转立刻生效

-(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
}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月13日 0

暂无评论

推荐阅读
Feeln6qFyXuE