55

アプリが読み込まれると、現在の向きがわからないようです。

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationPortrait) {
    NSLog(@"portrait");// only works after a rotation, not on loading app
}

デバイスを回転させると正しい向きになりますが、アプリを読み込んだときに向きを変えずに使用すると[[UIDevice currentDevice] orientation]、現在の向きがわからないようです。

アプリを最初にロードするときにこれを確認する別の方法はありますか?

4

16 に答える 16

66

編集:あなたの質問を読み間違えました。これにより、特定の向きでアプリケーションを起動できます。起動時の向きを把握しようとしていることに気付きました。

のステータス バーの向きを確認する方法がありますUIApplication

[[UIApplication sharedApplication] statusBarOrientation];

元の答え

plist ファイルで、アプリケーションが受け入れるデバイスの向きを設定してみてください。

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>

これは、アプリケーションが縦向き (下部のホーム ボタン)、横向き左、および横向き右をサポートしていることを示します。

次に、UIViewControllers で、shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)メソッドをオーバーライドして、アプリが回転する必要があるときに YES を返す必要があります。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

     return interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}

これにより、デバイスがサポートされている向きのいずれかにある場合、UIViewController に自動回転するように指示されます。逆さまの向き (ホーム ボタンが上にあるポートレート) もサポートしたい場合は、それを plist に追加し、このメソッドから YES を返すだけです。

それがどのように機能するか教えてください。

于 2011-05-04T19:17:17.370 に答える
11

私はこれがうまくいくと思います:

 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];UIInterfaceOrientation orientation = [UIDevice currentDevice].orientation;

UIDevice リファレンスによると:
引用:
「このプロパティの値は、beginGeneratingDeviceOrientationNotifications を呼び出すことによって方向通知が有効にされていない限り、常に 0 を返します」
当初、このプロパティには常に現在の方向が含まれていると想定していましたが、そうではないようです。通常、方向プロパティにアクセスする他の状況では、通知をオンにすることが舞台裏で処理されていると思います。そのため、これをアプリ デリゲート内で手動で行う必要があることは明らかではありませんでした。

于 2011-05-04T18:28:38.497 に答える
2

まだ誰も触れていないことの 1 つは、UIDeviceOrientation型をUIInterfaceOrientation変数に格納するということです。それらは異なっており、同等に扱うべきではありません。UIDeviceOrientationLeftが等しいことに注意してくださいUIInterfaceOrientationRight(インターフェイスはデバイスとは逆に回転するため)。

于 2011-10-03T01:33:14.110 に答える
2

次の通知を内部に挿入することでそれを行うことができます

-(void)viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkRotation:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];

次に、クラス内に次のメソッドを配置します

-(void)checkRotation:(NSNotification*)notification
{
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
    {
         //Do your textField animation here
    }
}

上記の方法は、iPad または iPhone のステータス バーの向きをチェックし、それに従って必要な向きでアニメーションを実行します。

于 2012-11-13T07:39:53.277 に答える
1

ステータス バーから向きを取得するには、plist ファイルですべての向きを有効にすることも重要です。

于 2015-03-06T21:24:33.160 に答える
0

これが本当の答えです。アプリの起動時、その向きは不明です。shouldAutorotateToInterfaceOrientation と supportedInterfaceOrientations を使用して、選択する向きを決定します。

iPhone 5.0 シミュレーターでサンプル アプリを起動し、以下のコードと「サポートされているインターフェイスの向き」を使用して 4 つの向きすべてで回転させる様子をご覧ください。

20:44:08.218 RotationTestApp Supported orientation: Portrait
20:44:08.222 RotationTestApp Supported orientation: Portrait (upside-down)
20:44:08.225 RotationTestApp Supported orientation: Landscape (home button on the right)
20:44:08.225 RotationTestApp Supported orientation: Landscape (home button on the left)
20:44:08.226 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (current device orientation: UIDeviceOrientationUnknown, interface orientation wants: UIInterfaceOrientationPortrait)
20:44:08.237 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (current device orientation: UIDeviceOrientationUnknown, interface orientation wants: UIInterfaceOrientationPortrait)
20:44:08.239 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (current device orientation: UIDeviceOrientationUnknown, interface orientation wants: UIInterfaceOrientationPortrait)
20:44:08.240 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (current device orientation: UIDeviceOrientationUnknown, interface orientation wants: UIInterfaceOrientationPortrait)
20:44:09.817 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationLandscapeLeft)
20:44:09.833 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationLandscapeLeft)
20:44:11.030 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationPortraitUpsideDown)
20:44:11.040 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationPortraitUpsideDown)
20:44:12.599 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationLandscapeRight)
20:44:12.609 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationLandscapeRight)
20:44:13.301 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationPortraitUpsideDown)

多くのコード スニペットを見てきましたが、一般的に十分に機能するものはありません (iPad と iPhone、iOS 5.0 以降)。

try-this-try-that をいじる代わりに、ルート VC に次のコードを配置します。

#define ToNSString_BEGIN(T) \
NSString* T##ToNSString(T valueParameter) { \
switch (valueParameter) {

#define ToNSString_VALUE(value) \
case value: return @#value

#define ToNSString_END(T) \
} \
return @"(unknown)"; \
}

// NSString* UIInterfaceOrientationToNSString(UIInterfaceOrientation);
ToNSString_BEGIN(UIInterfaceOrientation);
ToNSString_VALUE(UIInterfaceOrientationPortrait);           // 1
ToNSString_VALUE(UIInterfaceOrientationPortraitUpsideDown); // 2
ToNSString_VALUE(UIInterfaceOrientationLandscapeLeft);      // 3
ToNSString_VALUE(UIInterfaceOrientationLandscapeRight);     // 4
ToNSString_END  (UIInterfaceOrientation);

// NSString* UIDeviceOrientationToNSString(UIDeviceOrientation);
ToNSString_BEGIN(UIDeviceOrientation);
ToNSString_VALUE(UIDeviceOrientationUnknown);               // 0
ToNSString_VALUE(UIDeviceOrientationPortrait);              // 1
ToNSString_VALUE(UIDeviceOrientationPortraitUpsideDown);    // 2
ToNSString_VALUE(UIDeviceOrientationLandscapeLeft);         // 3
ToNSString_VALUE(UIDeviceOrientationLandscapeRight);        // 4
ToNSString_VALUE(UIDeviceOrientationFaceUp);                // 5
ToNSString_VALUE(UIDeviceOrientationFaceDown);              // 6
ToNSString_END  (UIDeviceOrientation);



// Change this custom method to alter auto-rotation behavior on all supported iOS versions and platforms.
- (BOOL)allowAutoRotate:(UIInterfaceOrientation)interfaceOrientation
{
    NSUInteger interfaceOrientationAsMask = (1<<interfaceOrientation);
    return interfaceOrientationAsMask & [self supportedInterfaceOrientations];
}

// Reads from the project's-Info.plist
- (NSUInteger)supportedInterfaceOrientations
{
    static NSUInteger orientationsResult;

    if (!orientationsResult) {
        NSArray *supportedOrientations = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UISupportedInterfaceOrientations"];

        for (id orientationString in supportedOrientations) {
            if ([orientationString isEqualToString:@"UIInterfaceOrientationPortrait"]) {
                orientationsResult |= UIInterfaceOrientationMaskPortrait;
                NSLog(@"Supported orientation: Portrait");
            } else if ([orientationString isEqualToString:@"UIInterfaceOrientationPortraitUpsideDown"]) {
                orientationsResult |= UIInterfaceOrientationMaskPortraitUpsideDown;
                NSLog(@"Supported orientation: Portrait (upside-down)");
            } else if ([orientationString isEqualToString:@"UIInterfaceOrientationLandscapeRight"]) {
                orientationsResult |= UIInterfaceOrientationMaskLandscapeRight;
                NSLog(@"Supported orientation: Landscape (home button on the left)");
            } else if ([orientationString isEqualToString:@"UIInterfaceOrientationLandscapeLeft"]) {
                orientationsResult |= UIInterfaceOrientationMaskLandscapeLeft;
                NSLog(@"Supported orientation: Landscape (home button on the right)");
            } else {
                NSLog(@"Unrecognized orientation '%@' in mainBundle plist, key UISupportedInterfaceOrientations", orientationString);
            }
        }
    }
   return orientationsResult;
}

// iOS 6+ (not yet used in 6.0.1)
- (BOOL)shouldAutorotate
{
    UIDeviceOrientation interfaceOrientationFromDevice = [UIDevice currentDevice].orientation;
    BOOL result = [self allowAutoRotate:interfaceOrientationFromDevice];
    NSString *currentDeviceOrientation = UIDeviceOrientationToNSString(interfaceOrientationFromDevice);
    NSLog(@"shouldAutorotate: %s (current orientation %@)", result ? "YES" : "NO", currentDeviceOrientation);
    return result;
}

// iOS 2.0 - 5.1 (iOS 6+ deprecated, 6.0.1 still works)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    NSString* orientationString;
    UIDeviceOrientation interfaceOrientationFromDevice = [UIDevice currentDevice].orientation;

    if ((int)interfaceOrientation != (int)interfaceOrientationFromDevice) {
        orientationString = [NSString stringWithFormat:@"current device orientation: %@, interface orientation wants: %@",
                             UIDeviceOrientationToNSString(interfaceOrientationFromDevice),
                             UIInterfaceOrientationToNSString(interfaceOrientation)
                             ];
    } else {
        orientationString = [NSString stringWithFormat:@"device orientation: %@", UIDeviceOrientationToNSString(interfaceOrientationFromDevice)
                             ];
    }

    BOOL result = [self allowAutoRotate:interfaceOrientation];
    NSLog(@"shouldAutorotateToInterfaceOrientation: %s (%@)",
          result ? "YES" : "NO",
          orientationString);
    return result;
}

現在の方向を使用しないセグエ アニメーションのしつこい問題がまだあります。私の推測では、各 VC をサブクラス化し、プッシュにオリエンテーションを配置し、ポップにデリゲートを通知することが最善の方法であると思います。

また重要:

shouldAutorotateToInterfaceOrientation が機能しない

横向きモードの tabBarController と navigationControllers、エピソード II

于 2013-01-13T05:08:11.460 に答える
0

これを試してみてください。それは私のための仕事です。その時点で危険なことに、didfinishedlaunch メソッドはデバイスの向きを検出しませんでした。デフォルトではポートレートとして表示されます。それで。統計バーの向きを確認するために使用しています。私はこのコードをテストします。appdeleget の didfinishedlaunch メソッドに入れてください。

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

if(orientation == 0) {//Default orientation
    //UI is in Default (Portrait) -- this is really a just a failsafe.

    NSLog("for portrait");


}else if(orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{

    NSLog("portrait");
}else if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{

    NSLog("Landscap");
}
于 2015-09-24T09:54:05.667 に答える
0

すべてを試しましたが、良い結果は得られませんでした。iPad を使用しているので、すべての作業を splitViewController メソッドに任せて、barButton を無効にしました。

ポートレートの場合:

- (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController: (UIPopoverController *)pc { NSlog(@"portrait");}

風景の場合:

- (void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem{ NSlog(@"landscape");}

これは常にロード時に機能します。

于 2011-11-07T19:31:39.403 に答える
0

加速度計で読み取り値を取得してみてください。

于 2011-05-04T18:17:43.280 に答える
0

私はまだiPhone 4用のこの作業コードスニペットを使用しています:

-(void)deviceOrientationDidChange:(NSNotification *)notification{

//Obtaining the current device orientation
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];    

int value = 0;

if(orientation == UIDeviceOrientationPortrait)
{
    value = 0;

}else if(orientation == UIDeviceOrientationLandscapeLeft)
{
    value = 90;

}else if(orientation == UIDeviceOrientationLandscapeRight)
{

    value = -90;

}

CGAffineTransform cgCTM = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(value));
[photoImageView setTransform:cgCTM];

}

于 2012-05-30T12:32:17.197 に答える
0

問題は[UIDevice currentDevice]orientation]、デバイスの向きが間違って報告されることがあるということです。

代わりに[[UIApplication sharedApplication]statusBarOrientation]which is を使用するUIInterfaceOrientationので、それを確認するには、 を使用する必要がありますUIInterfaceOrientationIsLandscape(orientation)

お役に立てれば。

于 2012-05-09T16:07:05.717 に答える
-1

上記の誰もが非常に有効な回答を投稿しました:しかし、更新として:Appleの見解:UIStatusBarの向きを使用して、デバイスの現在の向きを読み取る必要があります:

viewDidLoadデバイスの現在の向きを確認する方法の 1 つは、メソッド 内で int 値を使用することです。

    int orientationType = [[UIDevice currentDevice] orientation];

ここで、次のことを考慮してください。. . - 1 = 縦向き (右上) - 2 = 縦向き逆さま - 3 = 横向き (右) - 4 = 横向き (左)

IF次に、方向が検出された後にステートメントを使用してメソッドを呼び出すことができます。

これが誰かの役に立てば幸いです

于 2013-09-19T20:54:19.173 に答える