2009-07-21 5 views
12

私のプログラムでは、回転に基づいて物を動かしていますが、全体の回転はしません。私が使用している:回転前にiPhoneの方向を検出するにはどうすればいいですか

static UIDeviceOrientation previousOrientation = UIDeviceOrientationPortrait; 

- (void)applicationDidFinishLaunching:(UIApplication *)application { 
    [window addSubview:viewController.view]; 
    [window makeKeyAndVisible]; 
    [[NSNotificationCenter defaultCenter] addObserver:self 
      selector:@selector(didRotate:) 
      name:@"UIDeviceOrientationDidChangeNotification" object:nil]; 

} 

- (void) didRotate:(NSNotification *)notification{ 
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 

    [self doRotationStuff:orientation: previousOrientation]; 
previousOrientation = orientation; 

} 

これは、プログラムが起動されたときに、限り働き、デバイスの向きは縦向きですが、[自己doRotationStuff]は変更が相対ますので、初期の向きは、上下逆さま風景であるかどう以前の方向との差異に

起動時、またはデバイスを回転させる直前のいずれかで向きを検出する方法はありますか?

答えて

3

更新日:

ので、コメントの議論から、向きが実際に最初に変化するまで、あなたが[UIDevice currentDevice].orientationに頼ることができないことが表示されます。もしそうなら、あなたは生の加速度計の読みを取得することでおそらくそれをハックする可能性があります。

#define kUpdateFrequency 30 // Hz 
#define kUpdateCount 15 // So we init after half a second 
#define kFilteringFactor (1.0f/kUpdateCount) 

- (void)applicationDidFinishLaunching:(UIApplication *)app 
{ 
    [UIAccelerometer sharedAccelerometer].updateInterval = (1.0/kUpdateFrequency); 
    [UIAccelerometer sharedAccelerometer].delegate = self; 
    accelerometerCounter = 0; 
    ... 
} 

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)accel 
{ 
    // Average out the first kUpdateCount readings 
    // acceleration_[xyz] are ivars typed float 
    acceleration_x = (float)accel.x * kFilteringFactor + acceleration_x * (1.0f - kFilteringFactor); 
    acceleration_y = (float)accel.y * kFilteringFactor + acceleration_y * (1.0f - kFilteringFactor); 
    acceleration_z = (float)accel.z * kFilteringFactor + acceleration_z * (1.0f - kFilteringFactor); 

    accelerometerCounter++; 
    if (accelerometerCounter == kUpdateCount) 
    { 
     [self initOrientation]; 
     [UIAccelerometer sharedAccelerometer].delegate = nil; 
    } 
} 

- (void)initOrientation 
{ 
    // Figure out orientation from acceleration_[xyz] and set up your UI... 
} 

オリジナルの応答:

applicationDidFinishLaunching:中に正しい方向を返す[UIDevice currentDevice].orientationていますか?その場合は、その向きに従って初期UIを設定することができます。

後でそのプロパティが設定されない場合は、少し遅れてperformSelector:afterDelay:を試してUIを初期化してみてください。

このコードサンプルは、以下のケンドールの回答から、完全を期すためにここに追加されます。

[self performSelector:@selector(getOriented) withObject:nil afterDelay:0.0f]; 

Iはゼロ秒の遅延が十分であるかどうかわからないんだけど - これはgetOrientedのコードは中に実行することを意味します最初にイベント実行ループを通過します。加速度計の読み取り値がに登録されるまで、長く待つ必要があるかもしれません。

+0

は、[UIDevice currentDevice] .orientationは私がperformSelectorを使用する方法についての無知だUIDeviceOrientationUnknown –

+0

です。コード例がありますか? –

+0

私はあなたが言ったことを試しましたが、[self performSelector:@selector(getOriented)withObject:nil afterDelay:10.0f];何も効果がありませんでした –

-1

はあなたのコメントに応えて、私は、(本当にダニエルはここに信用に値するが)、私はより良いコメントよりも、ここでのコードを置くことができると思った:applicationDidFinishLaunchingで

[self performSelector:@selector(getOriented) withObject:nil afterDelay:0.0f]; 

次に、あなただけの必要呼び出す方法:

+0

私はあなたが言ったことを試みたが、[self performSelector:@selector(getOriented)withObject:nil afterDelay:10.0f]; 効果がありません –

+0

10秒後に呼び出されたときの向きは何ですか? 10秒後にオリエンテーションを設定するかのように思えます。そのときは、メインビューコントローラにオリエンテーションが何であるかを尋ねてください。 –

+0

それはUIDeviceOrientationUnknownでした –

1

モート、これらの答えは多少赤いニシンのようです。あなたがのUIViewControllerクラスの以下のビルトインメソッドを使用することはできませんなぜ私が見ることができません。

-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {} 

回転が発生した後、この方法は、(だけではなく、あなたに伝えshouldAutorotateToInterfaceOrientationで自動的に呼び出されます、それは程度です発生する)。手軽に、変数 'fromInterfaceOrientation'には以前の方向が含まれています。ドキュメントにもあるように、ビューのinterfaceOrientationプロパティが既に新しい方向に設定されていると仮定できるので、古い方向と新しい!

私が何かを見逃していて、すでにこの方法を使用できなくなってしまった場合は、謝罪します。あなたが上記の方法で無料で提供されている場合は、 "以前の方向"の変数を作成して保存しているのは奇妙なことです。

希望に役立ちます!加速器の測定値から、デバイスの向きを取得する方法について

+0

それは私がしたくないインターフェイスを回転することを計画していた場合にはうまくいくでしょう。 –

0

より完全な例では、ソリューションは、アクセルの測定値に依存しているとして、それはシミュレータ上で動作しませんので、あなたは、デバイス上で動作する必要がありますhere を見つけることができます。 ..まだシミュレータ上で動作する解決策を探しています。

12

場合によっては、UIViewControllerクラスのinterfaceOrientationプロパティが簡単なオプションになることがあります。これは回転前に正しいです。

+2

ありがとう!私はhtatフィールドが存在するのを知らなかった - 私は非常に有用ではなかったUIDeviceの方向を使用していた。 – Chris

+0

これが理由です。*** S.O Rocks! *** ...ナッギングFutzは、*咳* *単純な方向の問題を解決しようとしていた...手がかりがない...検索デラニーの答えが表示されます。 –

1

どのように指しているかを判断する必要がある場合は、UIの向きにこれを使用します。

100%わからないこれは正しいが、私の頭の上をオフに行くです:

[[[UIApplication sharedApplication] statusBar] orientation] 
1

は、ここでアプリが最初にロードの向きを得るための一つの方法だとUIDeviceOrientationはUIDeviceOrientationUnknownに設定されています。回転ビューのtransformプロパティを見ることができます。 afterDelay:applicationDidFinishLaunching中に

if(toInterface == UIDeviceOrientationUnknown) { 
    CGAffineTransform trans = navigationController.view.transform; 
    if(trans.b == 1 && trans.c == -1) 
     toInterface = UIDeviceOrientationLandscapeLeft; 
    else if(trans.b == -1 && trans.c == 1) 
     toInterface = UIDeviceOrientationLandscapeRight; 
    else if(trans.a == -1 && trans.d == -1) 
     toInterface = UIDeviceOrientationPortraitUpsideDown; 
    else 
     toInterface = UIDeviceOrientationPortrait; 
} 
+0

これは金です!それはあなたにビューの向きを与えます。私はこれを 'UIDeviceOrientationDidChangeNotification'で使用して私の以前の向きを検出しました。 – cnotethegr8

関連する問題