iPhone SDK でiPadアプリを製作中。
そこら中に落ちているサンプルを拾い集めながら何とか進めているけど、やはり理解しながら進めないと自分の実力にならないので、合間を見つけて勉強を進めている。
で、まずはメモとして、本体を回転した時にビューの向きを変える設定。というか、Controller.m に以下を書くだけ。
これで、自動でビューを回転してくれる。
でも、これだけではビューを画面にフィットさせることはできない。
回転時に画像のサイズ変更とかボタンの表示位置とかを修正する必要がある。
で、それを実装するのが、 willRotateToInterfaceOrientation() か didRotateFromInterfaceOrientation() で、画面を回転させる処理を開始する前と完了した後に呼び出される関数である。この他に、willAnimateFirstHalfOfRotationToInterfaceOrientation()、willAnimateSecondHalfOfRotationFromInterfaceOrientation() という処理もあり、回転のアニメーションの前半と後半に分けた場合にそれぞれ呼び出される。これ以外にもいくつかあるけど試す余裕はない・・・。
で、実際に回転した時に縦方向とか横方向を判定する方法についてだが、引数で取得する回転方向が、正常・反転・右・左のどれにあたるかを判別すればよい。
ここでいう、正常・反転・右・左とは、ホームボタンの位置と考えれば判りやすい。
正常: UIInterfaceOrientationPortrait
反転: UIInterfaceOrientationPortraitUpsideDown
右 : UIInterfaceOrientationLandscapeRight
左 : UIInterfaceOrientationLandscapeLeft
以下に、willRotateToInterfaceOrientation()、 didRotateFromInterfaceOrientation() でのテスト用コードを書く。実際にはこのif文をいじって、ボタンの位置とかを変えればよい。
そこら中に落ちているサンプルを拾い集めながら何とか進めているけど、やはり理解しながら進めないと自分の実力にならないので、合間を見つけて勉強を進めている。
で、まずはメモとして、本体を回転した時にビューの向きを変える設定。というか、Controller.m に以下を書くだけ。
// 自動回転を有効にするサブクラス
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
これで、自動でビューを回転してくれる。
でも、これだけではビューを画面にフィットさせることはできない。
回転時に画像のサイズ変更とかボタンの表示位置とかを修正する必要がある。
で、それを実装するのが、 willRotateToInterfaceOrientation() か didRotateFromInterfaceOrientation() で、画面を回転させる処理を開始する前と完了した後に呼び出される関数である。この他に、willAnimateFirstHalfOfRotationToInterfaceOrientation()、willAnimateSecondHalfOfRotationFromInterfaceOrientation() という処理もあり、回転のアニメーションの前半と後半に分けた場合にそれぞれ呼び出される。これ以外にもいくつかあるけど試す余裕はない・・・。
で、実際に回転した時に縦方向とか横方向を判定する方法についてだが、引数で取得する回転方向が、正常・反転・右・左のどれにあたるかを判別すればよい。
ここでいう、正常・反転・右・左とは、ホームボタンの位置と考えれば判りやすい。
正常: UIInterfaceOrientationPortrait
反転: UIInterfaceOrientationPortraitUpsideDown
右 : UIInterfaceOrientationLandscapeRight
左 : UIInterfaceOrientationLandscapeLeft
以下に、willRotateToInterfaceOrientation()、 didRotateFromInterfaceOrientation() でのテスト用コードを書く。実際にはこのif文をいじって、ボタンの位置とかを変えればよい。
// 回転を開始する前に行っておく処理
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
NSLog(@"ローテイト");
if(toInterfaceOrientation == UIInterfaceOrientationPortrait){
NSLog(@"will to Portrait");
}else if(toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ){
NSLog(@"will to UpsideDown");
}else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight ){
NSLog(@"will to LandscapeRight");
}else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){
NSLog(@"will to LandscapeLeft");
}else{
NSLog(@"will 何が起きた?!");
}
}
// 回転が完了した後に行っておく処理
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
if(fromInterfaceOrientation == UIInterfaceOrientationPortrait){
NSLog(@"did from Portrait");
}else if(fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ){
NSLog(@"did from UpsideDown");
}else if(fromInterfaceOrientation == UIInterfaceOrientationLandscapeRight ){
NSLog(@"did from LandscapeRight");
}else if(fromInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){
NSLog(@"did from LandscapeLeft");
}else{
NSLog(@"did 何が起きた?!");
}
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
NSLog(@"ローテイト");
if(toInterfaceOrientation == UIInterfaceOrientationPortrait){
NSLog(@"will to Portrait");
}else if(toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ){
NSLog(@"will to UpsideDown");
}else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight ){
NSLog(@"will to LandscapeRight");
}else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){
NSLog(@"will to LandscapeLeft");
}else{
NSLog(@"will 何が起きた?!");
}
}
// 回転が完了した後に行っておく処理
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
if(fromInterfaceOrientation == UIInterfaceOrientationPortrait){
NSLog(@"did from Portrait");
}else if(fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ){
NSLog(@"did from UpsideDown");
}else if(fromInterfaceOrientation == UIInterfaceOrientationLandscapeRight ){
NSLog(@"did from LandscapeRight");
}else if(fromInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){
NSLog(@"did from LandscapeLeft");
}else{
NSLog(@"did 何が起きた?!");
}
}
ここで、注意しなければならないのは、それぞれの関数で受け取る回転方向の情報である。
will~の方は、toInterfaceOrientation で、did~の方は、fromInterfaceOrientation を受け取っている。
"to"と"from"の違いは、意味通り、回転前はこれから回転する方向で、回転後はどの方向から回転されたかを示すのだ。
この引数を意識していなかったため、NSLogの結果の相違にパニクってしまった。
これで本体の回転については理解することができた。
関連記事として iPhone SDK 本体の回転 [[UIDevice currentDevice] orientation] で落とし穴が を追加。合わせてお読みください。
コメントする