日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iPhone之横竖屏与自动旋转

發布時間:2024/5/17 编程问答 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iPhone之横竖屏与自动旋转 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

iPhone的自動旋轉功能一共有三種方法:

  • 使用自動調整屬性處理旋轉,利用系統自動生成的代碼。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {// Return YES for supported orientationsreturn (interfaceOrientation == UIInterfaceOrientationPortrait);//系統默認不支持旋轉功能 }

要想讓系統自動實現旋轉功能僅需要實現上面的代碼,把return (interfaceOrientation == UIInterfaceOrientationPortrait)修改成為return OK即可。

修改后的代碼為:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {// Return YES for supported orientationsreturn OK; }

然后在使用自動調整屬性設計界面(Apple+3),指定要支持的方向即可。
在使用模擬仿真器的時候,要讓其自動旋轉只需Apple+ ->(<-)即可。

  • 在旋轉是重構視圖(即手動設置每一個控件的位置和大小,讓其重新排列)

第一種方法基本上不需要編寫代碼,這種方法就需要寫點代碼了,畢竟重新設置每一個控件的坐標了嘛。例如:

在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;@end

然后在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最終結果為:

  • 切換視圖

這種方法使用于比較復雜的界面,是需要分別設計橫向模式和縱向模式,然后在使用的過程中自動切換。
首先定義輸出口:(簡單描述,設計兩個視圖,一個定義為landscape,一個是portrait,一個為320x460,一個為480x300,每一個輸出口分別和每個視圖中的按鈕想關聯)

//用于切換的兩個ViewIBOutlet 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按鈕后的圖片:

總結

以上是生活随笔為你收集整理的iPhone之横竖屏与自动旋转的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。