19

ここで初心者の質問。

私は方向を検出します:

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

すべてがうまくてダンディで、報告された向きに従ってテキストフィールドとラベルを再配置します

if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown)

そしてelse{}他のすべてのために

私が最近発見した問題は、UIDeviceレポートUIDeviceOrientationFaceUpまたはUIDeviceOrientationFaceDown。この状況にどのように対処しますか?ポートレートとランドスケープのどちらで発生しているかUIDeviceOrientationFaceUpを知るにはどうすればよいですか?UIDeviceOrientationFaceDownデバイスが上向きか下向きかはわかりますが、すべてを縦向きと横向きのどちらに再配置する必要があるのか​​わかりません。

ありがとう!

4

3 に答える 3

33

Appleは、ビューのレイアウトにデバイスの向きを使用しないことをお勧めします。代わりに、各ビューコントローラーにはinterfaceOrientationプロパティUIApplicationがあり、statusBarOrientationプロパティがあり、どちらもビューのレイアウトに適した現在のインターフェイスの向きを返します。

変更を監視するために、willRotateToInterfaceOrientation:duration:UIViewControllerなどのメソッドが呼び出され、インターフェイスの向きの変更が発生したときに通知されるUIApplicationWillChangeStatusBarOrientationNotificationなどの通知があります。

于 2010-12-12T19:10:26.640 に答える
0

少し遅れて、うまくいけばそれは誰かを助けるでしょう。

iOS 6.0以降の場合:

1)回転通知を受信するには、ビューのセットアップ中に次のコードを設定します。

  // Set Listener to receive orientation notifications from the device
  [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(detectOrientation)
                                               name:UIDeviceOrientationDidChangeNotification
                                             object:nil];

2)ローテーション後に通知をキャッチするように次のコードを設定します。

-(void)detectOrientation{      
  switch ([[UIDevice currentDevice] orientation]) {
    case UIDeviceOrientationFaceDown:{
      NSLog(@"UIDeviceOrientationFaceDown");
      break;
    }
    case UIDeviceOrientationFaceUp:{
      NSLog(@"UIDeviceOrientationFaceUp");
      break;
    }
    case UIDeviceOrientationLandscapeLeft:{
      NSLog(@"UIDeviceOrientationLandscapeLeft");
      break;
    }
    case UIDeviceOrientationLandscapeRight:{
      NSLog(@"UIDeviceOrientationLandscapeRight");
      break;
    }
    case UIDeviceOrientationPortrait:{
      NSLog(@"UIDeviceOrientationPortrait");
      break;
    }
    case UIDeviceOrientationPortraitUpsideDown:{
      NSLog(@"UIDeviceOrientationPortraitUpsideDown");
      break;
    }
    case UIDeviceOrientationUnknown:{
      NSLog(@"UIDeviceOrientationUnknown");
      break;
    }
    default:{
      break;
    }
  }

}
于 2015-09-16T16:27:49.427 に答える
-1

私は同じ問題を抱えています

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

このメソッドは、7つの可能なデバイス位置状態の通知を生成します。

UIDeviceOrientationPortrait
UIDeviceOrientationPortraitUpsideDown
UIDeviceOrientationLandscapeLeft
UIDeviceOrientationLandscapeRight
UIDeviceOrientationFaceUp
UIDeviceOrientationFaceDown
UIDeviceOrientationUnknown

しかし、必要なのは最初の4つだけです。これは、この問題を解決するための私のアプローチです(コードが含まれています): ビューレイアウトを管理するためのUIDeviceOrientationの処理方法

于 2011-06-18T00:41:11.753 に答える