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.rhkb.cn/news/166010.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

揭开 Amazon Bedrock 的神秘面纱 | 基础篇

在 2023 年 4 月,亚马逊云科技曾宣布将 Amazon Bedrock 纳入使用生成式人工智能进行构建的新工具集。Amazon Bedrock 是一项完全托管的服务,提供各种来自领先 AI 公司(包括 AI21 Labs、Anthropic、Cohere、Stability AI 和 Amazon 等&#xf…

【COMP305 LEC 3 LEC 4】

LEC 3 A basic abstract model for a biological neuron 1. Weights of connections Neuron gets fired if it has received from the presynaptic neurons 突触前神经元 a summary impulse 脉冲, which is above a certain threshold. Signal from a single synapse突触 ma…

LiveQing视频点播流媒体RTMP推流服务功能-如何配置资源进行轮巡播放视频轮播分屏展示

LiveQing视频点播流媒体RTMP推流服务功能-如何配置资源进行轮巡播放视频轮播分屏展示 1、分屏展示2、右击节点新建分组3、配置轮播间隔(秒)4、选择资源5、轮巡播放6、停止分组播7、切换播放的流类型8、RTMP推流视频直播和点播流媒体服务 1、分屏展示 2、右击节点新建…

towxml的使用,在微信小程序中快速将markdown格式渲染为wxml文本

towxml的使用,在微信小程序中快速将markdown格式渲染为wxml文本 Towxml概述安装下载 Towxml在小程序中使用 towxml Towxml概述 towxml3.0 支持以下功能: ● echarts图表,默认禁用,需自行构建以开启此功能 ● LaTeX数学公式&#…

【linux】Linux 查看内存使用情况的几种方法汇总

文章目录 GUI 查看命令获取命令 free命令 vmstat命令 top命令 htop Linux 查看内存使用情况的几种方法包括使用 free 命令、top 命令、htop 命令、vmstat 命令和/proc/meminfo 文件。这些方法可以帮助用户了解系统内存的使用情况,包括总内存、已用内存、空闲内存、缓…

FPGA的64点FFT代码及报告,verilog快速傅里叶变换

名称:64点FFT快速傅里叶变换Radix4 软件:Quartus 语言:Verilog 代码功能: 使用verilog实现64-point Pipeline FFT处理器 FPGA代码资源下载网:hdlcode.com 代码下载: 名称:64点FFT快速傅里…

16.1 Socket 端口扫描技术

端口扫描是一种网络安全测试技术,该技术可用于确定对端主机中开放的服务,从而在渗透中实现信息搜集,其主要原理是通过发送一系列的网络请求来探测特定主机上开放的TCP/IP端口。具体来说,端口扫描程序将从指定的起始端口开始&#…

消失的它:网络层分片包中的第一个分片包去哪了?

在网络层IP包分片的过程中,遇到了大麻烦! 主机A: IP地址:192.168.0.10/24 MAC地址:02:00:00:00:00:10 主机B: IP地址:192.168.0.20/24 MAC地址:02:00:00:00:00:20 MTU:1…

16.The Tensor Product:Vector/Covector combinations

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

人人自媒体的时候,Ai绘画还值得踏入吗?

前言 先说结论,如果你不打算涉足自媒体,平时也从不上网发什么内容去展示自己的话,其实AI绘画对你来说意义不大。但如果你对自媒体感兴趣,会涉及发作品,发内容,甚至去设计图片,那么AI绘画值得你…

2023年“绿盟杯”四川省大学生信息安全技术大赛

pyfile 先check源码,没什么发现,接着进行目录扫描,扫到路径 /download 下载备份文件得到 www.zip,解压得到app.py 大致审一下代码: 在read目录下给file传参进行请求,如果这个东西存在就会读取出来 这里…

Node学习笔记之Node简介

一、Node简介 1.1、为什么学习Node(了解) 企业需求 增加自身职业竞争力 进一步理解 Web,并有助于明白后端开发 大前端必备技能 为了更好的学习前端框架 ... ... 1.2、Node是什么 Node.js是基于 Chrome的V8 JavaScript 引擎构建的JavaScript运行环境。 Node.js不是新…

渗透测试--JWT攻防(一)

JWT简介 JWT代表JSON Web Token,它是一种用于安全地在不同实体之间传递信息的开放标准(RFC 7519)。JWT通常用于身份验证和授权领域,以及在网络应用程序和服务之间传递声明(claims)信息。 JWT的常见用途包括…

【Redis】主从复制

目录 单点问题主从模式如何启动多个redis-server建立主从关系断开连接安全性只读传输延迟拓扑结构一主一从一主多从树型结构 主从复制的基本流程replicationid的作用offset的作用全量复制和部分复制全量复制的无硬盘模式关于runid和replid部分复制实时复制 单点问题 如果某个服…

07-React-redux和redux的使用

07.react-redux和redux的使用 1.redux的使用 1).redux的理解 a.redux是什么 redux是一个专门用于做状态管理的JS库(不是react插件库)。它可以用在react, angular, vue等项目中, 但基本与react配合使用。作用: 集中式管理react应用中多个组件共享的状态。 b.什么情况下需要使…

Lake Formation 和 IAM 之间的区别与联系

IAM 和 Lake Formation 都是 AWS 上的权限管理服务,且默认都是自动开启并生效的,只是如果你没有特别配置过它们,可能感觉不到它们的存在,特别是Lake Formation(后文简写为 LF),通常情况下都是“透明”的,但它确实在每次请求时进行了权限检查。本文会详细介绍一下两者之…

CPU飙高问题排查命令

1. 远程客户端连接服务器,top命令查看cpu占用最高的进程id 2. (top -H -p 进程pid) 命令: 找出进程里面线程占用CPU高的线程有哪些 ? 3. (printf 0x%x\n 线程id) 线程id转16进制 4. (./jstack PID | grep TID(十六进制) -A 30)

【rust/树莓派】使用rppalembedded-graphics控制st7789 LCD屏幕

说在前面 树莓派版本:4bLCD模块:ST7789V2 240*280 LCD树莓派系统:Linux raspberrypi 5.15.76-v8 #1597 SMP aarch64 GNU/Linuxrust版本:rustc 1.73.0 模块详情 某雪的1.69inch LCD模块,包含杜邦线 准备工作 树莓派…

Unity中Shader实现UI流光效果

文章目录 前言一、实现思路1:1、采集两张贴图,一张是主纹理,一张是扫光纹理2、在 v2f 定义一个二维变量 “uv2” 来存放 uv 偏移后的值3、在顶点着色器中,仿照之前的 uv 流动效果,与 _Time相乘后存放于 uv2 中4、最后,…

【Machine Learning】02-Advanced Learning Algorithms

02-Advanced Learning Algorithms 2. Advanced Learning Algorithms2.1 Neural Network2.1.1 概述2.1.2 Neural network model2.1.3 TensorFlow的实现2.1.4 Neural network implementation in Python2.1.5 强人工智能(AGI) 2.2 Vectorization2.2.1 矩阵使…