亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

iPhone開發之Rotation

系統 1684 0

?

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的上下邊距不變

對應的UI設置為

重新構造UI元素的大小和位置

- (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);
}
}


iPhone開發之Rotation


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲国产精品久久久久久网站 | 四虎在线最新地址公告 | 免费伊人 | 四虎国产精品影库永久免费 | 久久九九免费 | 伊人精品综合 | 亚洲一区精品视频在线 | 国产午夜成人无码免费看 | 呦女www| 国产在线不卡视频 | 日韩精品中文字幕久久 | 久久久国产99久久国产首页 | 亚洲一区 中文字幕 | 久久精品视频免费 | 日韩精品另类天天更新影院 | 国产日产欧产美一二三区 | 国产成人精品.一二区 | 国产日产久久高清欧美一区 | 成人日韩欧美 | er久99久热只有精品国产 | 看个毛片 | 四虎最新网 | 国产成人精品日本亚洲语言 | 伊人天伊人天天网综合视频 | 国产午夜影院 | 国产精品美女久久久久 | 国产成人性毛片 | 亚洲久草在线 | 久精品在线观看 | 老子影院无码午夜伦不卡 | 亚洲爱爱视频 | 成人性色生活片免费网 | 日本不卡高清免费v | 香蕉一级视频 | 亚洲线精品一区二区三区 | 久久成人在线 | 99久久中文字幕伊人情人 | 色国产在线视频一区 | 欧美一级毛片免费看 | 日韩精品区 | 美女操穴|