iPhone Screen Rotation

18
iPhone Screen Rotations

description

A presentation on basics of iPhone Screen Rotation

Transcript of iPhone Screen Rotation

Page 1: iPhone Screen Rotation

iPhone Screen Rotations

Page 2: iPhone Screen Rotation

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

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

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
Page 6: iPhone Screen Rotation
Page 7: iPhone Screen Rotation

By default, the iPhone Application project you created using Xcode supports a single orientation — portrait mode.

Page 8: iPhone Screen Rotation

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

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

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

Position : (20,20)Size : 233 by 37 points.

Position : (227, 243)Size : 233 by 37 points.

Page 12: iPhone Screen Rotation

#import <UIKit/UIKit.h>@interface ScreenRotationsViewController : UIViewController {

IBOutlet UIButton *btn;}@property (nonatomic, retain) UIButton *btn;

@end

Page 13: iPhone Screen Rotation

#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

- (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

Rotating the screen orientation during runtime when your application is running

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeLeft];

Page 16: iPhone Screen Rotation

Displaying the screen in a particular orientation when the View window is loaded

Page 17: iPhone Screen Rotation

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