1

デバイスの現在の向きを検出したい。

だから、私はコード行を使用しました

注: 登録していなくてもオリエンテーションが表示されます

-(IBAction) deviceOrientationChanged:(id) sender{
    NSLog(@"Device Orientation= %d",[[UIDevice currentDevice] orientation]);
}

向きを変更すると、ボタン クリック イベントでデバイスの向きが取得されます。

ただし、ドキュメントには、最初に登録してから上記のコードを使用する必要があると書かれています。

注:以下のコードでは、デバイスの向きの通知を受け取るようにデバイスを登録しました

-(IBAction) deviceOrientationChanged:(id) sender{

    if(![[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications]){
        //This statement is called very time when I click on the button. I expected to execute only once.
        [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    }
    NSLog(@"Device Orientation= %d",[[UIDevice currentDevice] orientation]);
}

そのため、デバイスの向きを取得するための完璧な方法を取得できません。

4

3 に答える 3

1

通知の登録は、通知の生成とは異なります。通常、アプリデリゲートまたは適切な場所でデバイスの向きの通知を使用することを計画している場合は、UIDeviceでbeginGeneratingDeviceOrientationNotificationsを呼び出し、通知が不要になったときに別の適切な場所でendGeneratingDeviceOrientationNotificationsを呼び出します。これは、deviceOrientaion通知の初期設定にすぎません。

これらの通知を受け取るには、コントローラーまたはオブジェクトUに通知センターが必要です。通知UIDeviceOrientationDidChangeNotificationに登録します。[[NSNotificationCenter defaultCenter] addObserver:myObjectセレクター:@selector(deviceRotated :) name:UIDeviceOrientationDidChangeNotificationオブジェクト:nil]

Uが不要な場合、またはオブジェクトの割り当て解除時に、通知センターから削除します。

于 2012-12-20T17:27:54.150 に答える
1

条件が間違っていると思います。Uはif([[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications])のように持っています

if( ! [[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications])

于 2012-12-18T23:48:51.727 に答える
0

UIViewControllerデバイスがローテーション通知を生成するのを止めることはできません。こちらの質問をご覧ください。

リンク先のドキュメントが参照しているのはNSNotificationCenter通知です。

于 2012-12-18T16:20:48.077 に答える