iPhone Screen Rotation
-
Upload
richa-jain -
Category
Technology
-
view
151 -
download
0
description
Transcript of iPhone Screen Rotation
iPhone Screen Rotations
UIInterfaceOrientationPortrait◦ Displays the screen in portrait mode
UIInterfaceOrientationPortraitUpsideDown◦ Displays the screen in portrait mode but with the Home
button at the top of the screen.
UIInterfaceOrientationLandscapeLeft◦ Displays the screen in landscape mode with the Home
button on the left.
UIInterfaceOrientationLandscapeRight◦ Displays the screen in landscape mode with the Home
button on the right.
Apple did a great job of hiding the complexities of autorotation in the iPhone OS and in the UIKit, so implementing this behavior in your own iPhone applications is actually quite easy.
Autorotation is specified in the view controller, so if the user rotates the phone, the active view controller will be asked if it’s OK to rotate to the new orientation .
If the view controller responds in the affirmative, the application’s window and views will be rotated, and the window and view will get resized to fit the new orientation.
By default, the iPhone Application project you created using Xcode supports a single orientation — portrait mode.
If you want to support screen orientations other than thedefault portrait mode, you can do so by overriding the
shouldAutorotateToInterfaceOrientation: method in a View Controller.
This event is commented out by default in the screenRotationsViewController.m file:
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{// Return YES for supported orientations//return (interfaceOrientation ==
UIInterfaceOrientationPortrait);return YES;
}
To support portrait mode and landscape mode in both directions but not rotation to the upside down portrait mode, we could do this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation !=UIInterfaceOrientationPortraitUpsideDown);}
As a general rule, UIInterfaceOrientationPortraitUpsideDownis discouraged by Apple, because if the phone rings while it is being held upside down, the phone is likely to remain upside down when it’s answered
This is pretty much the most basic thing you need to do to allow for auto-rotation but there are additional methods you can override that can be very useful. These methods are
◦ willRotateToInterfaceOrientation Pre-processing for your autorotation
◦ didRotateFromInterfaceOrientation Post-Processing for your autorotation
◦ willAnimateFirstHalfOfRotationToInterfaceOrientation◦ willAnimateSecondHalfOfRotationFromInterfaceOrient
ation
Position : (20,20)Size : 233 by 37 points.
Position : (227, 243)Size : 233 by 37 points.
#import <UIKit/UIKit.h>@interface ScreenRotationsViewController : UIViewController {
IBOutlet UIButton *btn;}@property (nonatomic, retain) UIButton *btn;
@end
#import “ScreenRotationsViewController.h”@implementation ScreenRotationsViewController@synthesize btn;-(void) positionViews {
UIInterfaceOrientation destOrientation = self.interfaceOrientation;
if (destOrientation == UIInterfaceOrientationPortrait ||destOrientation == UIInterfaceOrientationPortraitUpsideDown) {//---if rotating to portrait mode---btn.frame = CGRectMake(20, 20, 233, 37);
} else {
//---if rotating to landscape mode---btn.frame = CGRectMake(227, 243, 233, 37);}
}
- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation) fromInterfaceOrientationduration:(NSTimeInterval) duration {
[self positionViews];}
-(void)viewDidLoad {
[self positionViews];[super viewDidLoad];
}-(void)dealloc {
[btn release];[super dealloc];
}
Rotating the screen orientation during runtime when your application is running
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeLeft];
Displaying the screen in a particular orientation when the View window is loaded
On iPhone and iPod touch, the Home screen is displayed in one orientation only, which is portrait, with the Home button at the bottom. This leads users to expect iPhone apps to launch in this orientation by default.
On iPad, the Home screen is displayed in all orientations, so users tend to expect iPad apps to launch in the device orientation they’re currently using.