iOS Getting initial device orientation with accelerometer - ios

I am working in an OpenGL ES game, c++ and iOS. I would like that the canvas axes be always as in default portait orientation, and also be able to know which is the initial device orientation and all the orientation changes.
The problem I have is that if I configure the XCode project to support the 4 orientations: 2 Portraits and 2 Landscapes, then the Frame which is embedded the OpenGL canvas has the axes depending the initial orientation. If I configure the project to support only Portrait Orientation, then the Application won't tell me if it is in Landscape.
I would like to find a way to do this as much programmatically as possible.

1) To Know programatically in iOS
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if(orientation == UIDeviceOrientationLandscapeLeft)
landscapeRight = false;
else if(orientation == UIDeviceOrientationLandscapeRight)
else if(orientation == UIDeviceOrientationPortraitUpsideDown)
else if(orientation == UIDeviceOrientationPortrait)
....
2) For Default portrait view
Restrict other orientations in your Xcode project's info.plist

Related

IOS 9: Set camera orientation as Landscape or disable autorotation

I need to set my camera orientation to landscape in iOS 9; my code works in iOS 7 and 8 but does not seem to work now.
In iOS 8, I call CameraViewController : UIImagePickerController, with setting ShouldAutoRotate >> return NO.
I have already tried this:
if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft)
{
[[UIDevice currentDevice]setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeRight] forKey:#"orientation"];
}
else
{
[[UIDevice currentDevice]setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:#"orientation"];
}
There is no change in UIDeviceOrientationLandscapeRight, but if UIDeviceOrientationLandscapeLeft, it's rotated 90 degrees but the size is not correct.
In the info.plist I have set it to only accept landscape orientation. What else should I do or might have missed?

Facing camera faceup UIDeviceOrientation issue when use UIImagePickerController

I have an app that works on landscape, already in the store, and decided to add a camera for a certain new feature in my next update.
with an iPad 2 the image inside the camera appears rotated 90 degrees in clockwise direction.
Once the picture is taken it appears it's saved correct position.
Needed is that always there should be a landscape mode,while taking picture and once image is clicked and saved.But image inside the camera appears to be in Portrait view. I also checked the device orientation i.e faceUp orientation.
I have used the below code to manage the orientation but could not resolve
- (BOOL)shouldAutorotate {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
// UIDeviceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIDeviceOrientationFaceUp )
{
return NO;
}
else if (orientation == UIDeviceOrientationFaceDown)
{
return NO;
}
return YES;
}

when iphone Device orientation is face up/down, can I tell if it´s landscape or portrait?

I got this code that if the device is in landscape left/right or upside down it rotates and shows another view controller. but if it´s in the orientation face up or face down, then how can I tell if it´s in landscape mode or portrait? cause I only want to rotate if it´s face up or down and in landscape mode
- (void)viewDidAppear:(BOOL)animated
{
UIDeviceOrientation orientation = [[UIDevice currentDevice]orientation];
NSLog(#"orientation %d", orientation);
if ((orientation == 2) || (orientation == 3) || (orientation == 4))
{
[self performSegueWithIdentifier:#"DisplayLandscapeView" sender:self];
isShowingLandscapeView = YES;
}
}
The interfaceOrientation property is deprecated since iOS 8.
And the helpers methods
UIDeviceOrientationIsPortrait(orientation)
UIDeviceOrientationIsLandscape(orientation)
won't help either, because they return false when orientation is .faceUp.
So I ended checking this way:
extension UIViewController {
var isPortrait: Bool {
let orientation = UIDevice.current.orientation
switch orientation {
case .portrait, .portraitUpsideDown:
return true
case .landscapeLeft, .landscapeRight:
return false
default: // unknown or faceUp or faceDown
guard let window = self.view.window else { return false }
return window.frame.size.width < window.frame.size.height
}
}
}
This is in a UIViewController extension, so I can revert to comparing the screen width and height if everything else fails.
I use window because if the current ViewController is embedded in a container, it may not reflect the global iPad orientation.
In UI code you usually should not depend on the device orientation but the user interface orientation. There's often a difference between them, for example when a view controller only supports portrait.
The most important difference for your case is that the interface orientation is never face up/down.
In your case you can just ask the view controller for the current user interface orientation: self.interfaceOrientation.
Your condition could be expressed somewhat like if (deviceOrientation is face up/down and interfaceOrientation is landscape)
Bear in mind that a device orientation landscape left means a user interface orientation landscape right.
Yes you can, the UIDeviceOrientation is an enum which contains:
UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait,
UIDeviceOrientationPortraitUpsideDown,
UIDeviceOrientationLandscapeLeft,
UIDeviceOrientationLandscapeRight,
UIDeviceOrientationFaceUp,
UIDeviceOrientationFaceDown
There are even two helpers:
UIDeviceOrientationIsPortrait(orientation)
UIDeviceOrientationIsLandscape(orientation)
Just cmd on UIDeviceOrientation to show the headerfile where the enum is declared.
There is one way to check it.
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
UIInterfaceOrientation statusBarOrientation =[UIApplication sharedApplication].statusBarOrientation;
Use the first one to check if the device is in faceup and second one will tell you if the device is in portrait or landscape.

iOS5, iOS6, rotation, event firing arbitrarily

I have an app that supports orientation changes and rotates accordingly. I listen to the event UIApplicationWillChangeStatusBarOrientationNotification. Now during the load of this app I add some views and set them up in my app, based on the current orientation.
in iOS 6, this works fine and the app responds and rotates properly, so the user can load in both landscape and portrait mode and code works fine.
in iOS 5, if I load the app in portrait mode, the app works fine, and once that load has been completed in portrait mode, and the UI is aligned and sized, it will respond to other orientation changes to landascape or portrait. The problem I have is this : When loading iOS 5 in landscape mode, and while physically laying the device with iOS 5 on a flat surface to ensure its landscape, I get a OrientationNotification that moves from Landscape to portrait ( although the device didn't change ).
So another device iOS 6 in the same experiment, loads properly and I don't get any weird events of rotation changes that didn't occur, but with iOS 5 I do get them!!
Any ideas?
I am supporting the orientation for both iOS's
- (BOOL)shouldAutorotate {
return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait ||
interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
It sounds like iOS 5 believes things should be portrait if, in fact, there is no physical orientation (i.e. flat up or down) and iOS 6 doesn't. For what it might be worth, to determine the orientation to display stuff when it matters, I use the actual device orientation when available and the status bar orientation when the device is flat. For instance:
// Get a useful screen orientation.
// If the device is physically in portrait or landscape then
// that is the orientation.
// If it is not, then it is probably flat on a table and use
// the orientation of the status bar which should be set to
// the last physical orientation.
//
// screen Orientation
//------------------------------------------------------------
+ (UIDeviceOrientation) screenOrientation {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation!=UIInterfaceOrientationPortrait
&& orientation!=UIInterfaceOrientationPortraitUpsideDown
&& orientation!=UIInterfaceOrientationLandscapeLeft
&& orientation != UIInterfaceOrientationLandscapeRight) {
// Not known at this time. Use the status bar orientation
orientation = [[UIApplication sharedApplication] statusBarOrientation];
}
return orientation;
}
I'm not sure that helps you directly, but perhaps in your notification handler you could check to see what the actual status bar orientation is. Or maybe the timing of the notification and the status-bar orientation change doesn't make that work.

Is it possible to find out if an app UI is in landscape orientation after a device has been set down on a surface?

I'm determining a frame offset for a keyboard if the user is holding iPad in landscape mode. I'm finding that I'm unable to properly set the offset if I'm adding content in landscape mode and then set the iPad flat on a surface. I'm using the code below to set the offset:
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) {
tableView.frame = CGRectMake(0,0,tableView.frame.size.width, tableView.frame.size.height-keyboardsize.height);
}
UIDeviceOrientations that I can check for are:
typedef enum {
UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait,
UIDeviceOrientationPortraitUpsideDown,
UIDeviceOrientationLandscapeLeft,
UIDeviceOrientationLandscapeRight,
UIDeviceOrientationFaceUp,
UIDeviceOrientationFaceDown
} UIDeviceOrientation;
(source: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html)
Is there any way for me to check/know if a user has been holding the app in landscape orientation (UIDeviceOrientationLandscapeLeft or UIDeviceOrientationLandscapeRight) and then set the device down (which should be UIDeviceOrientationFaceUp)
What you're interested in is not the device orientation, but the interface orientation. Try using self.interfaceOrientation (where self is your view controller). You can also get this from [[UIApplication sharedApplication] statusBarOrientation].
That being said, you probably shouldn't be using the interface orientation at all to find out where the keyboard is. The keyboard generates notifications when it appears and moves around, which include position information. Take a look at the UIKeyboardWillShowNotification and UIKeyboardWillChangeFrameNotification. They have position information in their userInfo dictionary under the UIKeyboardFrameBeginUserInfoKey and UIKeyboardFrameEndUserInfoKey keys which will tell you where the keyboard will be.

Resources