IOS屏幕旋转监听

IOS屏幕旋转

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.mfbz.cn/a/103775.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【Lua语法】字符串

Lua语言中的字符串是不可变值。不能像在C语言中那样直接改变某个字符串中的某个字符,但是可以通过创建一个新字符串的方式来达到修改的目的 print(add2(1 , 2 ,15,3))a "no one"b string.gsub(a , "no" , "on1111")print(a) print…

VisualStudio[WPF/.NET]基于CommunityToolkit.Mvvm架构开发

一、创建 "WPF应用程序" 新项目 项目模板选择如下&#xff1a; 暂时随机填一个目标框架&#xff0c;待会改&#xff1a; 二、修改“目标框架” 双击“解决方案资源管理器”中<项目>CU-APP, 打开<项目工程文件>CU-APP.csproj, 修改目标框架TargetFramew…

关于 LLM 和知识图谱、图数据库,大家都关注哪些问题呢?

自 LLM 系列文章《知识图谱驱动的大语言模型 Llama Index》、《Text2Cypher&#xff1a;大语言模型驱动的图查询生成》、《Graph RAG: 知识图谱结合 LLM 的检索增强》陆续和大家见面&#xff0c;以及《夜谈 LLM》主题直播同大家交流一番 LLM 和知识图谱、图数据库之后&#xff…

Shiro框架详解

权限管理 权限管理&#xff0c;一般指根据系统设置的安全策略或者安全规则&#xff0c;用户可以访问而且只能访问自己被授权的资源。 权限管理包括用户身份认证和授权两部分&#xff0c;简称认证授权。对于需要访问控制的资源用户首先经过身份认证&#xff0c;认证通过后用户…

NFS性能瓶颈分析

前言 atop – run it with -d option or press d to toggle the disk stats view. iostat – try it with the -xm 2 options for extended statistics, in megabytes, and in two-second intervals. iotop – top-like I/O monitor. Try it with the -oPa options to show the…

vue 数据劫持代理原理

function lianxi(){// vue 数据劫持代理let data {username:curry,age:33}//模拟组件的实例let _this {}//利用Object.defineProperty()for( let item in data){//console.log(item,data[item])Object.defineProperty(_this,item,{//get:用来获取扩展属性值的,当获取该属性值…

个人创业新机遇,零成本开启真人手办定制项目

桌上的日历变薄&#xff0c;2023年已经接近尾声&#xff0c;浅观这一年&#xff0c;您是否发现大家日常关注的重点有明显的变化&#xff0c;诸多社会事件和宏观数字的背后&#xff0c;潜藏着对经济的“不托底”&#xff0c;而当我们真正开始关注起用个人经济积累去对冲未来的不…

CMake 坑之添加文件后不修改CMakeList.txt,则文件加不进来

问题&#xff1a; 我在一个文件夹中加了一个.h和对应的cpp。 这里.h中的类会被导出。 但是怎么都导不出来。 我试了无数次的 clean and rebuild 都没用。 分析&#xff1a; 做了很多实验。发现是新建的文件没有被读取进来。 解决方法&#xff1a; 改进了下对应的CMakeL…

C语言:实现对单链表的反转 函数封装

需求&#xff1a; 实现对单链表的反转 代码实现&#xff1a; #include <stdio.h>typedef struct node{int data;struct node* next; }NODE;void PrintLink(NODE* phead) {NODE* p phead;while(p ! NULL){printf("%d ",p->data);p p->next;}printf(&…

Python实现双目标定、畸变矫正、立体矫正

一&#xff0c;双目标定、畸变矫正、立体矫正的作用 双目目标定&#xff1a; 3D重建和测距&#xff1a;通过双目目标定&#xff0c;您可以确定两个摄像头之间的相对位置和朝向&#xff0c;从而能够根据视差信息计算物体的深度&#xff0c;进行三维重建和测距。姿态估计&#xf…

JVM相关的面试题

一、什么是程序计数器 二、简要的介绍一下堆 三、什么是虚拟机栈 四、能不能解释下方法区 五、你听过直接内存吗&#xff1f; 六、什么是类加载器&#xff0c;类加载器有哪些 七、什么是双亲委派模型 八、JVM为什么采用双亲委派机制 九、类装载的执行过程 十、对象什么时候被垃…

蓝凌EIS智慧协同平台saveImg接口任意文件上传漏洞复现 [附POC]

文章目录 蓝凌EIS智慧协同平台saveImg接口任意文件上传漏洞复现 [附POC]0x01 前言0x02 漏洞描述0x03 影响版本0x04 漏洞环境0x05 漏洞复现1.访问漏洞环境2.构造POC3.复现 0x06 修复建议 蓝凌EIS智慧协同平台saveImg接口任意文件上传漏洞复现 [附POC] 0x01 前言 免责声明&…

75 应急响应-数据库漏洞口令检索应急取证箱

必须知识点 1.第三方应用由于是选择性安装&#xff0c;如何做好信息收集和漏洞探针也是获取攻击者思路的重要操作&#xff0c;除去本身漏洞外&#xff0c;提前预知或口令相关攻击也要进行筛选 2.排除三方应用攻击行为&#xff0c;自查漏洞分析攻击者思路&#xff0c;人工配合工…

《java 桌面软件开发》swing 以鼠标为中心放大缩小移动图片

swing 使用Graphic2D 绘制图片&#xff0c;要实现对图片进行缩放和自由拖动。 1.以鼠标所在的位置为中心&#xff0c;滚轮控制缩放 2.缩放后再支持鼠标拖动。 基本原理&#xff1a; 利用scale() 函数。进行缩放。但是要注意的地方是&#xff0c;如果是在 public void paintCom…

Docker搭建Plex流媒体服务并播放自己本地视频

Docker搭建Plex流媒体服务 安装Docker创建存储配置文件的目录创建Plex容器配置Plex设置媒体库访问Plex 1 介绍 Plex是一个流媒体服务器&#xff0c;可以轻松地将你的媒体文件库&#xff08;如电影、电视节目和音乐&#xff09;通过网络流式传输到各种设备上。 Plex 是一套媒体…

Android 系统架构

首语 由于工作内容的转变&#xff0c;使得我向Android系统方向转变&#xff0c;对于一个Android系统工程师&#xff0c;了解Android整个系统架构是必然的。本篇是Android系统学习的开篇&#xff0c;Android系统庞大且复杂&#xff0c;但是能对Android的认识更深&#xff0c;更…

2019年亚太杯APMCM数学建模大赛B题区域经济活力及其影响因素的分析与决策求解全过程文档及程序

2019年亚太杯APMCM数学建模大赛 B题 区域经济活力及其影响因素的分析与决策 原题再现 区域&#xff08;或城市或省级&#xff09;经济活力是区域综合竞争力的重要组成部分。近年来&#xff0c;为了提高经济活力&#xff0c;一些地区推出了许多刺激经济活力的优惠政策&#xf…

Day 10 python学习笔记

高阶函数 map( ) map()函数接收两个参数&#xff0c;一个是函数&#xff0c;一个是Iterable&#xff0c;map将传入的函数依次作用到序列的每个元素&#xff0c;并把结果作为新的Iterator返回。&#xff08;Iterator是一个可迭代对象&#xff09; 再简单点说&#xff1a;从可迭代…

LeetCode讲解篇之面试题 01.08. 零矩阵

文章目录 题目描述题解思路题解代码 题目描述 题解思路 遍历矩阵&#xff0c;若当前元素为零&#xff0c;则将该行和该列的第一个元素置零 遍历第一行&#xff0c;若当前元素为零&#xff0c;则将当前列置零 遍历第一列&#xff0c;若当前元素为零&#xff0c;则将当前行置零 …

整理指定文件夹下的所有文件,以类树状图显示并生成对应超链接

最近在整理家里学习资料的时候&#xff0c;由于年代久远&#xff0c;找不到我想要找的文件&#xff0c;windows文件搜索速度感觉太慢。于是想要生成一份类似文件索引的东西来显示所有资料&#xff0c;让我可以快速的找到需要的资料路径 直接上代码 import os import datetim…