user979331
user979331

Reputation: 11911

Objective-C [UIDevice currentDevice].orientation returns 5

I have code like this:

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;
    }

but sometimes the orientation does not match any of the conditions and it returns the number 5....is there a better way to handle orientations? or is there a condition I am missing?

Upvotes: 0

Views: 296

Answers (2)

EricS
EricS

Reputation: 9768

Don't confuse UIDeviceOrientation with UIInterfaceOrientation. The first is for raw device values and the second is from a viewController's orientation.

UIDeviceOrientation comes UIDevice.currentDevice.orientation and UIInterfaceOrientation comes from UIViewController.interfaceOrientation (now deprecated).

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
};

Upvotes: 4

Earl Grey
Earl Grey

Reputation: 7466

Just count the case lines (start with 0) to 5 and see the description.

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

}

And it doesn't matter a posted a swift version..it's equivalent in Obj-C.

Upvotes: 0

Related Questions