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

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條評論
主站蜘蛛池模板: 天天躁天天碰天天看 | 亚洲韩精品欧美一区二区三区 | 久久精品国产亚洲 | 日韩精品一区二区三区四区 | 国产中文字幕在线观看 | 免费a视频在线观看 | 久久国内精品自在自线观看 | 久草视频免费在线看 | 亚洲国产综合自在线另类 | 日韩免费视频观看 | 婷婷自拍| 国产成人精品午夜在线播放 | 日韩伊人 | 日本在线观看一级高清片 | 五月开心六月伊人色婷婷 | 伊人狼人视频 | 四色成人| 成年女人午夜毛片免费看 | 伊人久久综合谁合综合久久 | 四虎免费影院ww4164h | 欧美乱码 | 尹人久久| 黄频网站在线观看视频 | 深夜福利视频网站 | 99久久精品免费看国产高清 | 亚洲狠狠成人综合网 | 国产精品久久久久影视不卡 | 四虎永久免费观看 | 久青草香蕉精品视频在线 | 亚洲国产精品免费 | 国产一区在线免费观看 | 欧美日韩在线播放一区二区三区 | 久久久久香蕉视频 | 四虎国产精品免费观看 | 91精品国产三级在线观看 | 日本中文字幕视频在线看 | 国产在线麻豆精品 | 久久优| 天天做天天爽爽快快 | 久久毛片久久毛 | 久久九九有精品国产56 |