Objective-C [UIDevice currentDevice] .orientation返回5

时间:2016-03-06 23:07:42

标签: objective-c

我有这样的代码:

if ([UIDevice currentDevice].orientation == UIInterfaceOrientationPortrait)
    {
        _prevLayer.frame = CGRectMake(40, 220, 300, 150);
        _prevLayer.connection.videoOrientation = AVCaptureVideoOrientationPortrait;
    }
    else if ([UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeLeft)
    {
        _prevLayer.frame = CGRectMake(0, 150, self.view.frame.size.width, 100);
        _prevLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeLeft;
    }
    else if ([UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeRight)
    {
        _prevLayer.frame = CGRectMake(0, 250, self.view.frame.size.width, 100);
        _prevLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
    }

但有时方向与任何条件都不匹配,它返回数字5 ....是否有更好的方法来处理方向?还是我缺少一个条件?

2 个答案:

答案 0 :(得分:4)

请勿将UIDeviceOrientationUIInterfaceOrientation混淆。第一个是原始设备值,第二个是viewController的方向。

UIDeviceOrientationUIDevice.currentDevice.orientationUIInterfaceOrientation来自UIViewController.interfaceOrientation(现已弃用)。

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    UIDeviceOrientationUnknown,
    UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom
    UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top
    UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right
    UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left
    UIDeviceOrientationFaceUp,              // Device oriented flat, face up
    UIDeviceOrientationFaceDown             // Device oriented flat, face down
};

typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
};

答案 1 :(得分:0)

只需将案例行(从0开始)计算为5并查看说明。

public enum UIDeviceOrientation : Int {

case Unknown
case Portrait // Device oriented vertically, home button on the bottom
case PortraitUpsideDown // Device oriented vertically, home button on the top
case LandscapeLeft // Device oriented horizontally, home button on the right
case LandscapeRight // Device oriented horizontally, home button on the left
case FaceUp // Device oriented flat, face up
case FaceDown // Device oriented flat, face down

}

在Obj-C中发布一个快速版本并不重要。