iphone screen rotation
DESCRIPTION
A presentation on basics of iPhone Screen RotationTRANSCRIPT
![Page 1: iPhone Screen Rotation](https://reader035.vdocuments.us/reader035/viewer/2022062303/55820f85d8b42ac7728b462a/html5/thumbnails/1.jpg)
iPhone Screen Rotations
![Page 2: iPhone Screen Rotation](https://reader035.vdocuments.us/reader035/viewer/2022062303/55820f85d8b42ac7728b462a/html5/thumbnails/2.jpg)
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.
![Page 3: iPhone Screen Rotation](https://reader035.vdocuments.us/reader035/viewer/2022062303/55820f85d8b42ac7728b462a/html5/thumbnails/3.jpg)
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.
![Page 4: iPhone Screen Rotation](https://reader035.vdocuments.us/reader035/viewer/2022062303/55820f85d8b42ac7728b462a/html5/thumbnails/4.jpg)
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.
![Page 5: iPhone Screen Rotation](https://reader035.vdocuments.us/reader035/viewer/2022062303/55820f85d8b42ac7728b462a/html5/thumbnails/5.jpg)
![Page 6: iPhone Screen Rotation](https://reader035.vdocuments.us/reader035/viewer/2022062303/55820f85d8b42ac7728b462a/html5/thumbnails/6.jpg)
![Page 7: iPhone Screen Rotation](https://reader035.vdocuments.us/reader035/viewer/2022062303/55820f85d8b42ac7728b462a/html5/thumbnails/7.jpg)
By default, the iPhone Application project you created using Xcode supports a single orientation — portrait mode.
![Page 8: iPhone Screen Rotation](https://reader035.vdocuments.us/reader035/viewer/2022062303/55820f85d8b42ac7728b462a/html5/thumbnails/8.jpg)
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;
}
![Page 9: iPhone Screen Rotation](https://reader035.vdocuments.us/reader035/viewer/2022062303/55820f85d8b42ac7728b462a/html5/thumbnails/9.jpg)
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
![Page 10: iPhone Screen Rotation](https://reader035.vdocuments.us/reader035/viewer/2022062303/55820f85d8b42ac7728b462a/html5/thumbnails/10.jpg)
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
![Page 11: iPhone Screen Rotation](https://reader035.vdocuments.us/reader035/viewer/2022062303/55820f85d8b42ac7728b462a/html5/thumbnails/11.jpg)
Position : (20,20)Size : 233 by 37 points.
Position : (227, 243)Size : 233 by 37 points.
![Page 12: iPhone Screen Rotation](https://reader035.vdocuments.us/reader035/viewer/2022062303/55820f85d8b42ac7728b462a/html5/thumbnails/12.jpg)
#import <UIKit/UIKit.h>@interface ScreenRotationsViewController : UIViewController {
IBOutlet UIButton *btn;}@property (nonatomic, retain) UIButton *btn;
@end
![Page 13: iPhone Screen Rotation](https://reader035.vdocuments.us/reader035/viewer/2022062303/55820f85d8b42ac7728b462a/html5/thumbnails/13.jpg)
#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);}
}
![Page 14: iPhone Screen Rotation](https://reader035.vdocuments.us/reader035/viewer/2022062303/55820f85d8b42ac7728b462a/html5/thumbnails/14.jpg)
- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation) fromInterfaceOrientationduration:(NSTimeInterval) duration {
[self positionViews];}
-(void)viewDidLoad {
[self positionViews];[super viewDidLoad];
}-(void)dealloc {
[btn release];[super dealloc];
}
![Page 15: iPhone Screen Rotation](https://reader035.vdocuments.us/reader035/viewer/2022062303/55820f85d8b42ac7728b462a/html5/thumbnails/15.jpg)
Rotating the screen orientation during runtime when your application is running
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeLeft];
![Page 16: iPhone Screen Rotation](https://reader035.vdocuments.us/reader035/viewer/2022062303/55820f85d8b42ac7728b462a/html5/thumbnails/16.jpg)
Displaying the screen in a particular orientation when the View window is loaded
![Page 17: iPhone Screen Rotation](https://reader035.vdocuments.us/reader035/viewer/2022062303/55820f85d8b42ac7728b462a/html5/thumbnails/17.jpg)
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.
![Page 18: iPhone Screen Rotation](https://reader035.vdocuments.us/reader035/viewer/2022062303/55820f85d8b42ac7728b462a/html5/thumbnails/18.jpg)