0

別のビューを作成するために、ユーザーがアプリを起動したときのデバイスの向きを知りたいです。私が奇妙だと思うのは、以下のとおりです。

- (void)viewDidLoad
{
    if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
        NSLog(@"1");
    }
    if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {
        NSLog(@"2");
    }
    if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {
        NSLog(@"3");
    }
    if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown) {
        NSLog(@"4");
    }
}

誰も印刷されていません!私はiPadシミュレーターでそれを行いましたが、向きはUIDeviceOrientationPortraitである必要があると思います。なぜこれが起こるのですか?向きを正しく知る方法は?

4

5 に答える 5

1

使ってみて

[UIApplication sharedApplication].statusBarOrientation

それ以外の

[UIDevice currentDevice].orientation
于 2012-05-16T12:41:08.230 に答える
0

シミュレーターが向きの状態になれない場合があります (コンピューターを実際に回転させることはできないため、意味がありません...) UIDeviceOrientationUnknown

于 2012-05-16T12:39:40.777 に答える
0

ドキュメントまたは UIDevice の状態:

Orientation プロパティを使用して現在の向きを取得するか、UIDeviceOrientationDidChangeNotification 通知に登録して変更通知を受け取ります。これらの手法のいずれかを使用して方向データを取得する前に、beginGeneratingDeviceOrientationNotifications メソッドを使用してデータ配信を有効にする必要があります。

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html

オリエンテーション通知の生成を開始していないため、不明なオリエンテーションが発生する可能性があります。

また、方向を取得したときに受け取ったものを正確に確認するために、値を viewDidLoad に記録する必要があります。それは、さらなる調査の良い出発点になるでしょう。

于 2012-05-16T12:41:50.607 に答える
0
Use this :-

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {      
if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)     
{       
 //code for portrait   

 }            
else      
{         //code for Landscape 

  }     
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationPortrait ||interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown); 
} 
  • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation は uiviewcontroller のデリゲート メソッドです。
于 2012-05-16T12:41:55.533 に答える
0

docsによると、 のorientationプロパティは、が最初に呼び出されない限りUIDevice、常に 0 (つまり ) を返します。このメソッドは、加速度計を有効にし、通知の変更を配信し、シングルトンのプロパティを更新します。UIDeviceOrientationUnknown-beginGeneratingDeviceOrientationNotificationsorientationUIDevice

于 2012-05-16T12:42:12.990 に答える