Iphone屏幕旋转

该示例是想在手机屏幕方向发生改变时重新定位视图(这里是一个button)

1.创建一个View—based Application项目,并在View窗口中添加一个Round Rect Button视图,通过尺寸检查器设置其位置,然后单击View窗口右上角的箭头图标来旋转窗口方向,重新定位button,这两个位置随便定义,只要能区分在不同位置即可,记住这两个位置的数据,因为在代码里面会用到。

2.在.h头文件里面定一个UIButton,并添加两个方法,后面会解释这两个方法:

[cpp] view plaincopyprint?
  1. #import <UIKit/UIKit.h>   
  2.   
  3.   
  4. @interface ChangeOrientation : UIViewController {  
  5.     IBOutlet UIButton *mybutton;  
  6.       
  7. }  
  8. @property(nonatomic,retain)UIButton *mybutton;  
  9.   
  10. -(void)positionViews;  
  11.   
  12. -(IBAction)makeChange;  
  13. @end  
#import <UIKit/UIKit.h>
@interface ChangeOrientation : UIViewController {
IBOutlet UIButton *mybutton;
}
@property(nonatomic,retain)UIButton *mybutton;
-(void)positionViews;
-(IBAction)makeChange;
@end

3.要向让手机支持所有旋转方向,必须修改自动生成的方法,让其return YES:

[cpp] view plaincopyprint?
  1. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  2. {  
  3.     return YES;  
  4. }  
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}

4.添加一个根据当前屏幕的方向改变button位置的方法,该方法在.h头文件定义过:

[cpp] view plaincopyprint?
  1. //根据当前的屏幕方向改变button的位置   
  2. -(void)positionViews{  
  3.     UIInterfaceOrientation destorientation = self.interfaceOrientation;  
  4.     if (destorientation == UIInterfaceOrientationPortrait ||   
  5.         destorientation == UIInterfaceOrientationPortraitUpsideDown) {  
  6.         mybutton.frame = CGRectMake(20, 20, 233, 37);  
  7.   
  8.     }else{  
  9.         mybutton.frame = CGRectMake(227, 243, 233, 37);  
  10.   
  11.     }  
  12.           
  13. }  
//根据当前的屏幕方向改变button的位置
-(void)positionViews{
UIInterfaceOrientation destorientation = self.interfaceOrientation;
if (destorientation == UIInterfaceOrientationPortrait || 
destorientation == UIInterfaceOrientationPortraitUpsideDown) {
mybutton.frame = CGRectMake(20, 20, 233, 37);
}else{
mybutton.frame = CGRectMake(227, 243, 233, 37);
}
}

5.当屏幕正在旋转的时候需要处理如下事件,这样就可以调用前面定义的方法positionViews方法改变button的位置:

(补充:willAnimateFirstHalfOfRotationToInterfaceOrientation:事件是在View窗口开始旋转前促发)

[cpp] view plaincopyprint?
  1. //当屏幕旋转到一半的时候促发的方法   
  2. -(void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration{  
  3.     [self positionViews];  
  4.      
  5. }  
//当屏幕旋转到一半的时候促发的方法
-(void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration{
[self positionViews];
}
6.在窗口加载完毕后调用positionViews方法来定位当前屏幕方向的button的位置:

[cpp] view plaincopyprint?
  1. - (void)viewDidLoad  
  2. {  
  3.     [self positionViews];  
  4.     [super viewDidLoad];  
  5. }  
- (void)viewDidLoad
{
[self positionViews];
[super viewDidLoad];
}

7.添加一个button点击方法(该方法在.h头文件中定义过),当点击这个button的时候动态改变屏幕的方向,代码如下:

[cpp] view plaincopyprint?
  1. //点击button动态改变屏幕方向   
  2. -(IBAction)makeChange{  
  3.     [[UIDevice currentDevice]setOrientation:UIInterfaceOrientationLandscapeLeft];  
  4.   
  5. }  
//点击button动态改变屏幕方向
-(IBAction)makeChange{
[[UIDevice currentDevice]setOrientation:UIInterfaceOrientationLandscapeLeft];
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/388340.shtml

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

相关文章

先进的NumPy数据科学

We will be covering some of the advanced concepts of NumPy specifically functions and methods required to work on a realtime dataset. Concepts covered here are more than enough to start your journey with data.我们将介绍NumPy的一些高级概念&#xff0c;特别是…

lsof命令详解

基础命令学习目录首页 原文链接&#xff1a;https://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316599.html 简介 lsof(list open files)是一个列出当前系统打开文件的工具。在linux环境下&#xff0c;任何事物都以文件的形式存在&#xff0c;通过文件不仅仅可以访问常规…

Xcode中捕获iphone/ipad/ipod手机摄像头的实时视频数据

目的&#xff1a;打开、关闭前置摄像头&#xff0c;绘制图像&#xff0c;并获取摄像头的二进制数据。 需要的库 AVFoundation.framework 、CoreVideo.framework 、CoreMedia.framework 、QuartzCore.framework 该摄像头捕抓必须编译真机的版本&#xff0c;模拟器下编译不了。 函…

统计和冰淇淋

Photo by Irene Kredenets on UnsplashIrene Kredenets在Unsplash上拍摄的照片 摘要 (Summary) In this article, you will learn a little bit about probability calculations in R Studio. As it is a Statistical language, R comes with many tests already built in it, …

信息流服务器哪种好,选购存储服务器需要注意六大关键因素,你知道几个?

原标题&#xff1a;选购存储服务器需要注意六大关键因素&#xff0c;你知道几个&#xff1f;信息技术的飞速发展带动了整个信息产业的发展。越来越多的电子商务平台和虚拟化环境出现在企业的日常应用中。存储服务器作为企业建设环境的核心设备&#xff0c;在整个信息流中承担着…

t3 深入Tornado

3.1 Application settings 前面的学习中&#xff0c;在创建tornado.web.Application的对象时&#xff0c;传入了第一个参数——路由映射列表。实际上Application类的构造函数还接收很多关于tornado web应用的配置参数。 参数&#xff1a; debug&#xff0c;设置tornado是否工作…

vml编辑器

<HTML xmlns:v> <HEAD> <META http-equiv"Content-Type" content"text/html; Charsetgb2312"> <META name"GENERATOR" content"网络程序员伴侣(Lshdic)2004"> <META name"GENERATORDOWNLOADADDRESS&q…

对数据仓库进行数据建模_确定是否可以对您的数据进行建模

对数据仓库进行数据建模Some data sets are just not meant to have the geospatial representation that can be clustered. There is great variance in your features, and theoretically great features as well. But, it doesn’t mean is statistically separable.某些数…

15 并发编程-(IO模型)

一、IO模型介绍 1、阻塞与非阻塞指的是程序的两种运行状态 阻塞&#xff1a;遇到IO就发生阻塞&#xff0c;程序一旦遇到阻塞操作就会停在原地&#xff0c;并且立刻释放CPU资源 非阻塞&#xff08;就绪态或运行态&#xff09;&#xff1a;没有遇到IO操作&#xff0c;或者通过某种…

arduino消息服务器,在C(Arduino IDE)中将API链接消息解析为服务器(示例代码)

我正在使用Arduino IDE来编程我的微控制器&#xff0c;它有一个内置的Wi-Fi芯片(ESP8266 NodeMCU)&#xff0c;它连接到我的互联网路由器&#xff0c;然后有一个特定的IP(就像192.168.1.5)。所以我想通过添加到链接的消息发送命令(和数据)&#xff0c;然后链接变为&#xff1a;…

不提拔你,就是因为你只想把工作做好

2019独角兽企业重金招聘Python工程师标准>>> 我有个朋友&#xff0c;他30出头&#xff0c;在500强公司做技术经理。他戴无边眼镜&#xff0c;穿一身土黄色的夹克&#xff0c;下面是一条常年不洗的牛仔裤加休闲皮鞋&#xff0c;典型技术高手范。 三 年前&#xff0c;…

python内置函数多少个_每个数据科学家都应该知道的10个Python内置函数

python内置函数多少个Python is the number one choice of programming language for many data scientists and analysts. One of the reasons of this choice is that python is relatively easier to learn and use. More importantly, there is a wide variety of third pa…

C#使用TCP/IP与ModBus进行通讯

C#使用TCP/IP与ModBus进行通讯1. ModBus的 Client/Server模型 2. 数据包格式及MBAP header (MODBUS Application Protocol header) 3. 大小端转换 4. 事务标识和缓冲清理 5. 示例代码 0. MODBUS MESSAGING ON TCP/IP IMPLEMENTATION GUIDE 下载地址&#xff1a;http://www.modb…

Hadoop HDFS常用命令

1、查看hdfs文件目录 hadoop fs -ls / 2、上传文件 hadoop fs -put 文件路径 目标路径 在浏览器查看:namenodeIP:50070 3、下载文件 hadoop fs -get 文件路径 保存路径 4、设置副本数量 -setrep 转载于:https://www.cnblogs.com/chaofan-/p/9742633.html

SAP UI 搜索分页技术

搜索分页技术往往和另一个术语Lazy Loading&#xff08;懒加载&#xff09;联系起来。今天由Jerry首先介绍S/4HANA&#xff0c;CRM Fiori和S4CRM应用里的UI搜索分页的实现原理。后半部分由SAP成都研究院菜园子小哥王聪向您介绍Twitter的懒加载实现。 关于王聪的背景介绍&#x…

万彩录屏服务器不稳定,万彩录屏 云服务器

万彩录屏 云服务器 内容精选换一换内网域名是指仅在VPC内生效的虚拟域名&#xff0c;无需购买和注册&#xff0c;无需备案。云解析服务提供的内网域名功能&#xff0c;可以让您在VPC中拥有权威DNS&#xff0c;且不会将您的DNS记录暴露给互联网&#xff0c;解析性能更高&#xf…

针对数据科学家和数据工程师的4条SQL技巧

SQL has become a common skill requirement across industries and job profiles over the last decade.在过去的十年中&#xff0c;SQL已成为跨行业和职位描述的通用技能要求。 Companies like Amazon and Google will often demand that their data analysts, data scienti…

C# 读取CAD文件缩略图(DWG文件)

//C# 读取CAD文件缩略图&#xff08;DWG文件&#xff09; 杨航收集技术资料&#xff0c;分享给大家 //2010-09-04 16:34:58| 分类&#xff1a; C# |字号 订阅//在不使用任务插件的情况下读取DWG文件的缩略图&#xff0c;以便在没有安装AutoCAD的计算机上浏览。using System;u…

全排列算法实现

版权声明&#xff1a;本文为博主原创文章&#xff0c;未经博主允许不得转载。 https://blog.csdn.net/summerxiachen/article/details/605796231.全排列的定义和公式&#xff1a; 从n个数中选取m&#xff08;m<n&#xff09;个数按照一定的顺序进行排成一个列&#xff0c;叫…

14.并发容器之ConcurrentHashMap(JDK 1.8版本)

1.ConcurrentHashmap简介 在使用HashMap时在多线程情况下扩容会出现CPU接近100%的情况&#xff0c;因为hashmap并不是线程安全的&#xff0c;通常我们可以使用在java体系中古老的hashtable类&#xff0c;该类基本上所有的方法都采用synchronized进行线程安全的控制&#xff0c;…