?
iPhone或iPad可以支持4種朝向
??? UIInterfaceOrientationPortrait
??? UIInterfaceOrientationPortraitUpsideDown
??? UIInterfaceOrientationLandscapeLeft
??? UIInterfaceOrientationLandscapeRight
究竟支持哪幾個朝向,由view controller的shouldAutorotateToInterfaceOrientation函數來指定,每當設備的朝向發生變化時,這個方法都會被調用。
對于iPhone程序,應該防止用戶在通話時拿倒電話,不支持UIInterfaceOrientationPortraitUpsideDown
因此函數實現為:
- (BOOL)shouldAutorotateToInterfaceOrientation:? (UIInterfaceOrientation) interfaceOrientation {
????? return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
對于iPad,應該支持所有的朝向,因此函數實現為
- (BOOL)shouldAutorotateToInterfaceOrientation:? (UIInterfaceOrientation) interfaceOrientation {
????? return true;
}
實現對Rotation的支持有三種方法:
1. 設置UI元素的Autosize Attribute,類似于.NET form的dock
2. 重新構造UI元素大小和位置
3. 不同的朝向使用不同的view
使用Autosize Attribute
Inner box中的紅色箭頭和UI元素的size有關,如果橫向的紅色的箭頭成為實線,則窗口尺寸變化時UI元素的寬度也會變化,如果橫向紅色箭頭為虛線,則窗口寬度變化時UI元素的寬度保持不變。垂直方向同理。
Inner box周圍的短線變成實線時,表明UI元素和它所在的view的邊緣的距離保持不變。
下圖中的UI對象的高度將會隨著view的高度而變化,同時UI元素相對于View的上下邊距不變
- (void)
willAnimateRotationToInterfaceOrientation
:? (UIInterfaceOrientation)
interfaceOrientation duration:(NSTimeInterval)duration {
?? if (interfaceOrientation == UIInterfaceOrientationPortrait
??????? || interfaceOrientation == 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);
?? }
}
切換View
為了支持兩個View,需要在View controll中生成兩套outlet
@interface SwapViewController : UIViewController {
????? UIView *landscape;
????? UIView *portrait;
????? // Foo
????? UIButton *landscapeFooButton;
????? UIButton *portraitFooButton;
????? // Bar
????? UIButton *landscapeBarButton;
????? UIButton *portraitBarButton;
}
在Interface builder中刪除Xcode自動生成的view,從library中拖兩個view 到main window上。分別命名為Portrait和Landscape.
選中File’s Owner,Control-drag 到Portrait和Landscape,連接outlet
動態加載view
#define degreesToRadians(x) (M_PI * (x) / 180.0)
- (void)
willAnimateRotationToInterfaceOrientation
: (UIInterfaceOrientation)
interfaceOrientation duration:(NSTimeInterval)duration {
if (interfaceOrientation == UIInterfaceOrientationPortrait) {
self.view = self.portrait;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(0));#define degreesToRadians(x) (M_PI * (x) / 180.0)
self.view.bounds = CGRectMake(0.0, 0.0, 320.0, 460.0);
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
self.view = self.landscape;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(-90));
self.view.bounds = CGRectMake(0.0, 0.0, 480.0, 300.0);
}
else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
self.view = self.portrait;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(180));
self.view.bounds = CGRectMake(0.0, 0.0, 320.0, 460.0);
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
self.view = self.landscape;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90));
self.view.bounds = CGRectMake(0.0, 0.0, 480.0, 300.0);
}
}
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
