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.pswp.cn/news/388340.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388340.shtml
英文地址,請注明出處:http://en.pswp.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;…