20

デバイスが縦向きモードか横向きモードかを調べようとしています。デバイスが上を向いていない場合、私のコードは非常にうまく機能します。顔を上に向けた場合(および向き== 5)、縦向きと横向きを区別しません。UIDeviceOrientationがFaceUpの場合、横向き/縦向きの「向き」を決定する方法はありますか?

私のコード:

UIDeviceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];

NSLog(@"orientation: %d", interfaceOrientation);

if (interfaceOrientation == UIDeviceOrientationIsLandscape(interfaceOrientation)) {
    NSLog(@"LANDSCAPE!!!");
}

if (interfaceOrientation == UIDeviceOrientationIsPortrait(interfaceOrientation)) {
    NSLog(@"PORTRAIT!!!");
}
4

3 に答える 3

42

あなたは混同しないでくださいUIDeviceOrientation、そしてUIInterfaceOrientation、それらは異なっていますが、それらの宣言によって示されるように関連しています

typedef enum {
   UIDeviceOrientationUnknown,
   UIDeviceOrientationPortrait,
   UIDeviceOrientationPortraitUpsideDown,
   UIDeviceOrientationLandscapeLeft,
   UIDeviceOrientationLandscapeRight,
   UIDeviceOrientationFaceUp,
   UIDeviceOrientationFaceDown
} UIDeviceOrientation;

typedef enum {
   UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
   UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
   UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
   UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
} UIInterfaceOrientation;

UIDeviceOrientationデバイスの向きを教えてくれます。UIInterfaceOrientationインターフェイスの向きを示し、によって使用されUIViewControllerます。UIInterfaceOrientation明らかに縦向きまたは横向きになりますが、値UIDeviceOrientationがあいまいになる可能性があります(、、UIDeviceOrientationFaceUp)。UIDeviceOrientationFaceDownUIDeviceOrientationUnknown

いずれの場合も[[UIDevice currentDevice] orientation]、デバイスの向きに関係なく、UIViewController interfaceOrientationプロパティが異なる可能性があるため、UIViewControllerの向きを決定しようとしないでください(たとえば、アプリが横向きにまったく回転しない場合は、可能[[UIDevice currentDevice] orientation]です)。UIDeviceOrientationLandscapeLeftviewController.interfaceOrientationUIInterfaceOrientationPortrait

更新: iOS 8.0以降、[UIViewController interfaceOrientation]非推奨になりました。ここで提供される代替手段はです[[UIApplication sharedApplication] statusBarOrientation]。これもを返しますUIInterfaceOrientation

于 2011-11-05T09:18:18.043 に答える
5

このコードスケルトンは、必要なデバイスと不要なデバイスの向きを処理するためのものです。私の場合は、UIDeviceOrientationUnknown、UIDeviceOrientationFaceUp、UIDeviceOrientationFaceDownを無視して、最後に許可された向きをキャッシュします。このコードはiPhoneおよびiPadデバイスを扱い、あなたに役立つ可能性があります。

- (void)modXibFromRotation {

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    NSString *device = [[UIDevice currentDevice]localizedModel];
    UIInterfaceOrientation cachedOrientation = [self interfaceOrientation];

    if ([device isEqualToString:@"iPad"]) {

        if (orientation == UIDeviceOrientationUnknown || 
            orientation == UIDeviceOrientationFaceUp || 
            orientation == UIDeviceOrientationFaceDown) {

                orientation = (UIDeviceOrientation)cachedOrientation;
        }

        if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {

            /* Your code */
        }

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

            /* Your code */     
        }
    }

    if ([device isEqualToString:@"iPhone"] || [device isEqualToString:@"iPod"]) {

        if (orientation == UIDeviceOrientationUnknown || 
        orientation == UIDeviceOrientationFaceUp || 
        orientation == UIDeviceOrientationFaceDown) {

            orientation = (UIDeviceOrientation)cachedOrientation;
        }

        if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {

             /* Your code */
        }

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

            /* Your code */
        }
    }
}
于 2012-01-04T03:38:26.927 に答える
1

iOS13の使用時点で

UIInterfaceOrientation orientation;
if (@available(iOS 13.0, *)) {
    orientation = self.window.windowScene.interfaceOrientation;
} else {
    orientation = [[UIApplication sharedApplication] statusBarOrientation];
}
于 2019-12-10T13:43:29.360 に答える