IOS屏幕旋转监听

news/2024/7/20 20:52:57 标签: ios, cocoa, macos

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

2.添加事件连接

 3.按钮点击事件实现

先添加三个IBAction

实现IBAction

使用旋转立刻生效

-(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
}


http://www.niftyadmin.cn/n/5110590.html

相关文章

bug记录(feign)——如何给feign加上请求头相关的参数,让生产者获得请求头中参数

问题描述 在微服务的调用中,如果消费者通过feign调用生产者,然后生产者方法中需要用到请求头,从请求头中获得token,但是feign默认是不携带请求头的,因此即使消费者请求feign的时候请求头中携带了token,也没…

【华为OD机试】分苹果

A、B两个人把苹果分为两堆,A希望按照他的计算规则等分苹果,他的计算规则是按照二进制加法计算,并且不计算进位 1259(1100 01019),B的计算规则是十进制加法,包括正常进位,B希望在满足A的情况下获取苹果重量…

转行做程序员,多晚都不晚

大家好啊,我是董董灿。 最近有不少小伙伴加我微信咨询一些问题,有同学想了解AI行业的现状,想着转行的,也有在校生想了解毕业后工作方向的,当然也有想学习编程知识的。 诚惶诚恐,没想到之前写的文章&#…

QMediaPlaylist 类使用教程

文章目录 1、简介2 、公共类型3、属性4、functions4.1、访问属性相关 function4.2、公共槽4.3、Signal4.4、其他方法 QT 官方文档参考地址:https://doc.qt.io/qt-5/qmediaplaylist.html 1、简介 moudleclass说明PyQt5.QtCore其他模块使用的核心非图形类QUrl用于处理…

关于ibatis的一些用法——1(基本语法)

关于ibatis的一些用法——1(基本语法) 1. 前言1.1 唠叨1.2 官网1.2.1 官网地址1.2.2 价值文档1.2.2.1 Data Mapper1.2.2.2 其他 1.3 价值 IT技术问答网站 2. 基本语法2.1 关于 in 的用法2.2 关于驼峰问题2.3 3. 1. 前言 1.1 唠叨 本不想整这些老框架的…

16.The Tensor Product:Vector/Covector combinations

本节将概括目前为止所学的张量积知识。并讨论一般张量,它可以由任意数量的向量和协向量的任意组合来生成。 同样,也是使用的非标准的符号。 (2,0)阶张量, 由两个向量生成的。 (1,2)阶张…

【912.排序数组】

目录 一、题目描述二、算法原理2.1快速排序2.2归并排序 三、代码实现3.1快排代码实现3.2归并代码实现 一、题目描述 二、算法原理 2.1快速排序 2.2归并排序 三、代码实现 3.1快排代码实现 class Solution { public:int getRandom(int left,int right,vector<int>&…

2.1.1BFS中的Flood Fill和最短路模型

1.池塘计数 农夫约翰有一片 N ∗ M N∗M N∗M 的矩形土地。 最近&#xff0c;由于降雨的原因&#xff0c;部分土地被水淹没了。 现在用一个字符矩阵来表示他的土地。 每个单元格内&#xff0c;如果包含雨水&#xff0c;则用”W”表示&#xff0c;如果不含雨水&#xff0c;…