| プログラム / 2009/07/03 (Fri) / 編集 |
まず「shouldAutorotateToInterfaceOrientation:」でYesを返します。
//本体の回転を検出し、Viewを回転させる
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
そうすると、これがYESだと自動的にWindowを回転し、「willRotateToInterfaceOrientation」や「didRotateFromInterfaceOrientation」
が呼ばれるようになります。
//本体の回転に会わせて処理を変える関数
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
//本体の方向に応じてViewを変更
switch (toInterfaceOrientation) {
case UIInterfaceOrientationPortrait:
NSLog(@"正位置");//ボタンが下
[pub showCenterView];//Viewを呼び出す(下記参照)
break;
case UIInterfaceOrientationPortraitUpsideDown:
NSLog(@"上下逆");
[pub showFlipView];
break;
case UIInterfaceOrientationLandscapeLeft:
NSLog(@"右回転");//NSLog(@"左が上");
[pub showRightView];
break;
case UIInterfaceOrientationLandscapeRight:
NSLog(@"左回転");//NSLog(@"右が上");
[pub showLeftView];
break;
default:
NSLog(@"Unkown");
break;
}
}
実際にViewを切り替える関数はこんな感じにしてみた
//本体を右に回転させたときに呼ばれる関数
-(void)showRightView{
//nowView : 現在一番上にあるView
//RootView : 親になるView
//rightViewController : 表示させたいViewのコントローラ
NSLog(@"[Public:showRightView]");
//現在一番上にあるViewを削除
[nowView removeFromSuperview];
//右回転させる
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
//あたらしいViewをRootViewに追加(一番上に表示)
[rootViewController.view addSubview:rightViewController.view];
//実行
[UIView commitAnimations];
//現在のViewとして登録
nowView = rightViewController.view;
}
この回転方向を表す長ったらしい引数はUIApplicationクラスのUIInterfaceOrientationコンスタントらしい
- UIInterfaceOrientationPortrait ホームボタンが下になるデフォルトの位置
- UIInterfaceOrientationPortraitUpsideDown 上下逆になる
- UIInterfaceOrientationLandscapeRight 本体を右に90度回転させたとき(ホームボタンが左)
- UIInterfaceOrientationLandscapeLeft 本体を左に90度回転させたとき(ホームボタンが右)
PR
トラックバック
URL :
コメント
