IPhone的自動旋轉功能一共有3中方法:html
1.使用自動調整屬性處理旋轉。框架
利用系統自動生成的代碼。ide
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);//系統默認不支持旋轉功能
}ui
要想讓系統自動實現旋轉功能僅須要實現上面的代碼,把return (interfaceOrientation == UIInterfaceOrientationPortrait)修改爲爲return OK便可。atom
修改後的代碼爲:url
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return OK;
}spa
而後在使用自動調整屬性設計界面(Apple+3),指定要支持的方向便可。.net
在使用模擬仿真器的時候,要讓其自動旋轉只需Apple+ ->(<-)便可。設計
2.在旋轉是重構視圖。(也即手動設置每個控件的位置和大小,讓其從新排列)orm
第一種方法基本上不須要編寫代碼,這種方法就須要寫點代碼了,畢竟從新設置每個控件的座標了嘛。
下面以我弄的一個爲例子,例子的名稱爲IP_05Autosize。
例如,首先看這個圖片:
在View中添加6個button,而後設定W和H分別爲125和125,這樣在橫向的時候這三個button是會重疊的,由於在橫向的時候Iphone 支持的顯示像素爲480x300,沒有狀態欄的狀況下是480x320.(在縱向時候顯示像素爲320x460,沒有狀態欄的狀況下是320x480)。
如今就須要寫代碼從新排列這六個按鈕了。
首先聲明變量:
在IP_05AutosizeViewController.h中添加以下的代碼:
#import <UIKit/UIKit.h>
@interface IP_05AutosizeViewController : UIViewController {
IBOutlet UIButton *button1;
IBOutlet UIButton *button2;
IBOutlet UIButton *button3;
IBOutlet UIButton *button4;
IBOutlet UIButton *button5;
IBOutlet UIButton *button6;
}
@property (nonatomic,retain)UIView *button1;
@property (nonatomic,retain)UIView *button2;
@property (nonatomic,retain)UIView *button3;
@property (nonatomic,retain)UIView *button4;
@property (nonatomic,retain)UIView *button5;
@property (nonatomic,retain)UIView *button6;
而後在IP_05AutosizeViewController.m中,添加實現方法。
@implementation IP_05AutosizeViewController
@synthesize button1;
@synthesize button2;
@synthesize button3;
@synthesize button4;
@synthesize button5;
@synthesize button6;
-(void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration
{
UIInterfaceOrientation to=self.interfaceOrientation;
if(to == UIInterfaceOrientationPortrait || to == UIInterfaceOrientationPortraitUpsideDown)
{
button1.frame = CGRectMake(20, 20, 125, 125);
button2.frame = CGRectMake(175, 20, 125, 125);
button3.frame = CGRectMake(20, 168, 125, 125);
button4.frame = CGRectMake(175, 168, 125, 125);
button5.frame = CGRectMake(20, 315, 125, 125);
button6.frame = CGRectMake(175, 315, 125, 125);
}
else
{
button1.frame = CGRectMake(20, 20, 125, 125);
button2.frame = CGRectMake(20, 155, 125, 125);
button3.frame = CGRectMake(177, 20, 125, 125);
button4.frame = CGRectMake(177, 155, 125, 125);
button5.frame = CGRectMake(328, 20, 125, 125);
button6.frame = CGRectMake(328, 155, 125, 125);
}
}
還須要修改- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation的代碼:
修改後爲:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait
|| interfaceOrientation == UIInterfaceOrientationLandscapeLeft
|| interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
而後在dealloc中釋放資源:
- (void)dealloc
{
[button1 release];
[button2 release];
[button3 release];
[button4 release];
[button5 release];
[button6 release];
[super dealloc];
}
到此代碼部分搞定,而後就是鏈接控制器和視圖了。這點應該比較簡單了。呵呵!
而後Build and Go最終結果爲:
3.切換視圖
這種方法使用於比較複雜的界面,是須要分別設計橫向模式和縱向模式,而後在使用的過程當中自動切換。
固然了這個也須要肯定輸出口和一些方法了。
首先定義輸出口:
(簡單描述,設計兩個視圖,一個定義爲landscape,一個是portrait,一個爲320x460,一個爲480x300,每個輸出口分別和每一個視圖中的按鈕想關聯)
//用於切換的兩個View
IBOutlet UIView *landscape;
IBOutlet UIView *portrait;
//Foo兩個View中的Foo按鈕
IBOutlet UIButton *landscapeFooButton;
IBOutlet UIButton *portraitFooButton;
//Bar兩個View中的Bar按鈕
IBOutlet UIButton *landscapeBarButton;
IBOutlet UIButton *portraitBarButton;
還須要File's Owner和兩個View想關聯。按住Ctrl將File's Owner拖到Portrait上面,在彈出灰色菜單上選擇Portrait同理選擇Landscape。而後在按住Ctrl將File's Owner拖到Landscape上面,在彈出的灰色菜單上選擇View,讓Landscape爲自啓動View。
而後是方法的實現:
-(void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)to duration:(NSTimeInterval)duration
{
if(to == UIInterfaceOrientationPortrait)
{
self.view = self.portrait;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degressToRadian(0));
self.view.bounds = CGRectMake(0.0, 0.0, 320.0, 460.0);
}
else if (to == UIInterfaceOrientationLandscapeLeft)
{
self.view = self.landscape;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degressToRadian(-90));
self.view.bounds = CGRectMake(0.0, 0.0, 460.0, 320.0);
}
else if (to == UIInterfaceOrientationPortraitUpsideDown)
{
self.view = self.portrait;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degressToRadian(180));
self.view.bounds = CGRectMake(0.0, 0.0, 320.0, 460.0);
}
else if (to == UIInterfaceOrientationLandscapeRight)
{
self.view = self.landscape;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degressToRadian(90));
self.view.bounds = CGRectMake(0.0, 0.0, 460.0, 320.0);
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
不要忘了在dealloc中釋放資源哦。
由於在上面的代碼中使用到了Core Graphics框架,所以要把該框架鏈接到該項目中,具體的方法是:在Resources上面點右鍵Add->Existing Frameworks。而後就是查找路徑了。我第一次就看錯了沒有找到,哎,作事情不下心呀!具體路徑爲:/Developer/Platforms /iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.1.sdk/System /Library/Frameworks/CoreGraphics.framework 呵呵,這個路徑是有點長很差找,記住不是在:/Developer/SDKs裏面了,我剛開始就是找到呢裏面了。呵呵!還有就是導入後會處理一個提示框, 不要Copy,Reference Type要選擇Relative to Current SDK而後add就OK了。
例子中還有一個方法是對View中的按鈕實現隱藏的,這個就比較簡單了!
具體方法爲:
-(IBAction)buttonPressed:(id)sender
{
if(sender == portraitFooButton||sender == landscapeFooButton)
{
portraitFooButton.hidden=YES;
landscapeFooButton.hidden=YES;
}
else
{
portraitBarButton.hidden=YES;
landscapeBarButton.hidden=YES;
}
}
呵呵,上圖兩張看看:
剛開始運行時的圖片
旋轉後而且按了Foo按鈕後的圖片