iPhone - Screen Rotation? - iphone

What's the difference between UIDeviceOrientation & UIInterfaceOrientation ?
Which one should I use to detect rotation on a UIView?

UIDeviceOrientation gives you information about the physical device itself while UIInterfaceOrientation tells you about the orientation of the views it is displaying. These do not need to match; when a user uses the orientation lock or if the the device orientation is face up.
You probably want to be checking UIInterfaceOrientation and the rotation methods on UIViewController to determine when views have been or should be rotated.

UIDeviceOrientation refers to the physical orientation of the device whereas UIInterfaceOrientation refers to the orientation of the user interface.
So what you need to set is the autoResizeMask so that the view is resized correctly when its superview is resized. Set flexibleWidth and flexibleHeight for the view. Check http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIDevice_Class/Reference/UIDevice.html
Also - refer to
UIDEVICE orientation
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]

Related

Different Orientation for Different Views?

I'm making an iOS application that has an interface in the portrait view.
However, when displaying web content I want the view to display in a landscape format, because I want the website test to display larger initially without the user needing to zoom.
Can I tell the program so present this view only in landscape?
Thank you.
looks like you want to force the orientation to landscape mode only..
heres my solution in MyViewController.h
add this code on top of MyViewController's #interface
//force orientation on device
#interface UIDevice (PrivateOrientation)
- (void) setOrientation:(UIInterfaceOrientation)orientation;
#end
then in the implementation file (MyViewController.m)
add this code inside viewWillAppear:
//change orientation of device
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeLeft];
this will force the device orientation to landscape mode left (or right depending what you want) if you want to go back to portrait mode after leaving the viewcontroller add this code inside viewWillDisappear:
//change orientation of device
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
finally implement shouldAutorotateToInterfaceOrientation: to force the view into landscape mode left or right (or both)
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
hope this helps :3

UIDeviceOrientation issue with iPad

I have an iPad in which I start at a landscape left orientation, however when I do:
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
it doesn't detect it as landscape left. I had to rotate it to portrait mode and then back to landscape left again, and then when I check the orientation, now it's correct. However, initially when it's launched it is not correct. Why is this? How do I resolve this?
Never use the Device Orientation if you would like to determine the Interface Orientation!
If you have a reference to the RootViewController of your main window, you could ask it for its interfaceOrientation.
If you don't, use [UIApplication sharedApplication].statusBarOrientation to determine the Interface Orientation.

iPhone rotation on demand

Is there a way to manually check current position of device AND ask device to automatically rotate it without waiting for user to actually rotate the device?
Currently I control the rotation with several IFs in -shouldAutorotateToInterfaceOrientation but in few situations I have to stop the view from rotating and enable it again and I don't want user to rotate the device twice to have the desired orientation.
for checking Orientation
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
then based on your condition you can change device orientation as you wish..
you can use :
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;
or what ever you want.
or you can go for following but this is not do
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait];

Inaccurate device orientation returned by UIDeviceOrientationDidChangeNotification

I'm listening to the UIDeviceOrientationDidChangeNotification in order to adjust my UI based on the device's orientation.
The problem is that the device orientation I'm getting from the notification seems inaccurate. If I start with the phone in portrait orientation and vertical (as if taking a picture with it) the orientation I get from the notification is correct. As the tilt of the phone approaches horizontal (as in laying flat on a table top) the orientation switches to landscape. This happens much before the phone is actually flat on the table. And this is without rotating the phone towards landscape at all. Is as if it had a preference for landscape.
When using other apps, like mail, I don't see this same behavior. It seems that mail only switches orientation once it's really sure you've gone to the new orientation.
Any help greatly appreciated.
Thanks,
I found my problem.
In viewWillAppear I have:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver: self selector: #selector(didChangeOrientation:) name: UIDeviceOrientationDidChangeNotification object:
[UIDevice currentDevice]];
And here is the notification handler:
- (void) didChangeOrientation: (id)object {
UIInterfaceOrientation interfaceOrientation = [[object object] orientation];
//DLog(#"val is %i", interfaceOrientation);
if (interfaceOrientation == UIDeviceOrientationLandscapeLeft || interfaceOrientation == UIDeviceOrientationLandscapeRight || interfaceOrientation == UIDeviceOrientationPortrait) {
[self orientInterface:interfaceOrientation];
}
By checking that the orientation is one of the two landscapes or the two portraits I'm ignoring the UIDeviceOrientationFaceUp and UIDeviceOrientationFaceDown orientations. That way setting the phone in one of this two orientations won't have an effect on my interface's orientation.
My problem was that I was no considering the faceUp and faceDown and was handling them both in an else statement which was assuming landscape.
If your only interest is your interface orientation (ie landscape or portrait orientation of the device), you should NOT use UIDevice:orientation (or the UIDeviceOrientation* constants if you will), but rather use the UIApplication:statusBarOrientation, which uses the UIInterfaceOrientation* constants.
I use the following code to check for landscape modus:
static inline bool isOrientationIsLandscape() {
return UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]);
}
And for portrait modus:
static inline bool isOrientationIsPortrait() {
return UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]);
}
This cost me a whole morning to figure out, because there is no UIDeviceOrientationFace[Up|Down] on the simulator, only on the real device. So my app worked on the simulator all the time, but on the actual device I had some undefined behavoir during testing every now and then.
Now it works on the actual device like it does on the simulator.
HTH
The notifications are just one way to get at it, you can also read out the accelerometer yourself and implement it in exactly the way you see fit (with a delay and a certain time of non-rotation for example).
Don't know if it's a power drain to get those readouts, but if so, use the notification to know when things are moving, and then fire up the accelometer-reading.

how to know the direction of rotation of UIImage in iPhone

i want to rotate the uiimage in the direction turned, i'm able to get the angle between touchesBegan and TouchesEnd points but how should i decide whether to rotate cockwise or anti-clockwise?
Plz help
Get the current orientation of the device and rotates as you wish.
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
orientation is UIDeviceOrientationLandscapeLeft and UIDeviceOrientationLandscapeRight if the device is on landscape.