72

iOS でデバイスの向きを検出する方法について質問があります。変更通知を受け取る必要はありません。現在のオリエンテーション自体だけです。これはかなり単純な質問のようですが、頭を包むことができませんでした。以下は私がこれまでに行ったことです:

UIDevice *myDevice = [UIDevice currentDevice] ;
[myDevice beginGeneratingDeviceOrientationNotifications];
UIDeviceOrientation deviceOrientation = myDevice.orientation;
BOOL isCurrentlyLandscapeView = UIDeviceOrientationIsLandscape(deviceOrientation);
[myDevice endGeneratingDeviceOrientationNotifications];

私の考えでは、これはうまくいくはずです。デバイスがデバイスの向きの通知を受信できるようにし、デバイスの向きを尋ねますが、機能せず、理由がわかりません。

4

14 に答える 14

120

本当に古いスレッドですが、本当の解決策はありません。

同じ問題が発生しましたが、UIDeviceOrientationの取得が常に一貫しているとは限らないことがわかったため、代わりに次を使用してください。

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

if(orientation == 0) //Default orientation 
    //UI is in Default (Portrait) -- this is really a just a failsafe. 
else if(orientation == UIInterfaceOrientationPortrait)
    //Do something if the orientation is in Portrait
else if(orientation == UIInterfaceOrientationLandscapeLeft)
    // Do something if Left
else if(orientation == UIInterfaceOrientationLandscapeRight)
    //Do something if right
于 2012-03-25T00:22:13.607 に答える
76

UIViewController の場合:

if (UIDeviceOrientationIsLandscape(self.interfaceOrientation))
{
    // 
}

UIViewの場合:

if (UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation))
{
    //
}

UIDevice.h:

#define UIDeviceOrientationIsPortrait(orientation)  ((orientation) == UIDeviceOrientationPortrait || (orientation) == UIDeviceOrientationPortraitUpsideDown)
#define UIDeviceOrientationIsLandscape(orientation) ((orientation) == UIDeviceOrientationLandscapeLeft || (orientation) == UIDeviceOrientationLandscapeRight)

更新日:

このコードを xxx-Prefix.pch に追加すると、どこでも使用できます。

// check device orientation
#define dDeviceOrientation [[UIDevice currentDevice] orientation]
#define isPortrait  UIDeviceOrientationIsPortrait(dDeviceOrientation)
#define isLandscape UIDeviceOrientationIsLandscape(dDeviceOrientation)
#define isFaceUp    dDeviceOrientation == UIDeviceOrientationFaceUp   ? YES : NO
#define isFaceDown  dDeviceOrientation == UIDeviceOrientationFaceDown ? YES : NO

利用方法:

if (isLandscape) { NSLog(@"Landscape"); }
于 2012-12-12T09:49:51.613 に答える
22

加速度計からデバイスの向きを直接取得したい場合は、 を使用します[[UIDevice currentDevice] orientation]ただし、アプリケーションの現在の向き (インターフェイスの向き)が必要な場合は、 を使用します[[UIApplication sharedApplication] statusBarOrientation]

于 2015-01-07T22:56:15.627 に答える
22

最初に探しているものについては、方向が変更された場合に通知を受け取る必要があります! this Thing を viewDidLoad のように設定できます

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(OrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];

デバイスの向きが変更されるたびに OrientationDidChange が呼び出され、向きごとに好きなことを行うことができます

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

    if(Orientation==UIDeviceOrientationLandscapeLeft || Orientation==UIDeviceOrientationLandscapeRight)
    {
    }
    else if(Orientation==UIDeviceOrientationPortrait)
    {
    }
}
于 2014-10-30T12:30:55.690 に答える
9

UIViewControllerinterfaceOrientationView Controllerの現在の向きを調べるためにアクセスできるプロパティがあります。

あなたの例に関しては、それはうまくいくはずです。それが機能していないと言うとき、それはどういう意味ですか? 期待したものと比べて、どのような結果が得られますか?

于 2011-04-05T23:15:25.573 に答える
4

UIViewcontroller の向きが特定の向きに固定されている場合、デバイスの向きに関連する情報を取得できないため、「UIDeviceOrientation」では満足できませんでした。そのため、 「UIInterfaceOrientation」を使用するのが正しい方法です。

"self.interfaceOrientation"を使用して UIViewController から向きを取得できますが、コードを因数分解する場合は、ビュー コントローラー (カスタム ビュー、カテゴリなど) の外側でこの種のテストを行う必要がある場合があります。rootviewControllerを使用して、コントローラーの外部の情報にアクセスできます。

if (UIInterfaceOrientationIsLandscape(view.window.rootViewController.interfaceOrientation)) {
}
于 2013-07-12T08:06:44.913 に答える
2

CoreMotion からのデータを使用して、方向ロックが有効になっているかどうかに関係なく、これを実現する方法があります。これはコードです:

#import <CoreMotion/CoreMotion.h> 

    CMMotionManager *cm=[[CMMotionManager alloc] init];
    cm.deviceMotionUpdateInterval=0.2f;
    [cm startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
                            withHandler:^(CMDeviceMotion *data, NSError *error) {

                            if(fabs(data.gravity.x)>fabs(data.gravity.y)){
                                    NSLog(@"LANSCAPE");
                                if(data.gravity.x>=0){
                                    NSLog(@"LEFT");
                                }
                                else{
                                    NSLog(@"RIGHT");
                                }

                        }
                        else{
                                NSLog(@"PORTRAIT");
                                if(data.gravity.y>=0){
                                    NSLog(@"DOWN");
                                }
                                else{

                                    NSLog(@"UP");
                                }

                            }

}];
于 2015-08-24T03:23:32.897 に答える
1

デバイスの向きのハードウェア ロックを解除しましたか? 私のiPad 1の端に1つあります。

于 2011-04-05T23:24:31.897 に答える
1

検出を容易にするための Swift 変数を次に示します。

let LANDSCAPE_RIGHT: Bool = UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight
let LANDSCAPE_LEFT: Bool = UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft
let LANDSCAPE: Bool = LANDSCAPE_LEFT || LANDSCAPE_RIGHT
let PORTRAIT_NORMAL: Bool = UIDevice.currentDevice().orientation == UIDeviceOrientation.Portrait
let PORTRAIT_REVERSE: Bool = UIDevice.currentDevice().orientation == UIDeviceOrientation.PortraitUpsideDown
let PORTRAIT: Bool = PORTRAIT_REVERSE || PORTRAIT_NORMAL
于 2016-01-20T00:33:17.310 に答える
0

これは、SwiftUI または通常の Swift オブジェクトで非常に簡単に使用できる、Combine を使用した私のソリューションです。シングルトン オブジェクト (静的インスタンス) は、この種の真にグローバルなオブジェクトの「環境」よりも優れています。

// Singleton object to keep the interface orientation (and any other global state)
class SceneContext: ObservableObject {
    @Published var interfaceOrientation = UIInterfaceOrientation.portrait
    static let shared = SceneContext()
}

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    ...
    func windowScene(_ windowScene: UIWindowScene, didUpdate previousCoordinateSpace: UICoordinateSpace, interfaceOrientation previousInterfaceOrientation: UIInterfaceOrientation, traitCollection previousTraitCollection: UITraitCollection) {
        SceneContext.shared.interfaceOrientation = windowScene.interfaceOrientation
    }
}

    // if you want to execute some code whenever the orientation changes in SwiftUI
    someView {
        ....
    }
    .onReceive(SceneContext.shared.$interfaceOrientation) { (orientation) in
        // do something with the new orientation
    }

    // if you want to execute some code whenever the orientation changes in a regular Swift object
    let pub = SceneContext.shared.$interfaceOrientation.sink(receiveValue: { (orientation) in
            // do something with the new orientation
            ...
        }) 

于 2021-04-24T23:54:55.633 に答える