[iOS]iPhoneの向きを変更する | Cocoa練習帳

[iOS]iPhoneの向きを変更する

Xcodeで、ターゲットのSummaryのiPhone/iPod Deployment InfoのSupported Device Orienttationsで、対応する向きを選択する。(下記の図の赤い丸で囲まれたボタン)古い資料では、Info.plistのUIInterfaceOrientationキーのカスタマイズが説明されているが、この操作によってInfo.plistが更新される。



BITZ Weblog-info.plist

四つの項目(Portrait, Upside Down, Landscape Left, Landscape Right)の選択順には意味があって、最初に選択された物が、起動時の画面の向きという事になるようだ。



ユーザーがiPhoneの向きを変更するのに追従して、自動で画面の向きを変更したい場合は、ビュー・コントローラのshouldAutorotateToInterfaceOrientation:メソッドをカスタマイズする。



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
BOOL result = YES;
if (interfaceOrientation == UIInterfaceOrientationPortrait)
result = YES;
else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
result = YES;
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight)
result = YES;
else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
result = NO;
return result;
}


例えば、上記の例は、上下が逆さま(UIInterfaceOrientationPortraitUpsideDown)以外が自動で向きが変わるというコードだ。



BITZ Weblog-UIInterfaceOrientationPortrait
BITZ Weblog-UIInterfaceOrientationLandscapeLeft

ソースコード
GitHubからどうぞ。

https://github.com/murakami/workbook/tree/master/ios/Autorotation - GitHub

関連情報
User Experience Coding How-To's

iOS Developer Libraryの情報です。