源码如下:
.h文件
#import <UIKit/UIKit.h>
#define degreesToRadian(x) (M_PI * (x) / 180.0)
@interface SwapViewController : UIViewController {
UIView *landscape;
UIView *portrait;
// Foo
UIButton *landscapeFooButton;
UIButton *portraitFooButton;
// Bar
UIButton *landscapeBarButton;
UIButton *portraitBarButton;
}
@property (nonatomic, retain) IBOutlet UIView *landscape;
@property (nonatomic, retain) IBOutlet UIView *portrait;
@property (nonatomic, retain) IBOutlet UIButton *landscapeFooButton;
@property (nonatomic, retain) IBOutlet UIButton *portraitFooButton;
@property (nonatomic, retain) IBOutlet UIButton *landscapeBarButton;
@property (nonatomic, retain) IBOutlet UIButton *portraitBarButton;
-(IBAction)buttonPressed:(id)sender;
@end
.m文件
#import "SwapViewController.h"
@implementation SwapViewController
@synthesize landscape;
@synthesize portrait;
@synthesize landscapeFooButton;
@synthesize portraitFooButton;
@synthesize landscapeBarButton;
@synthesize portraitBarButton;
-(IBAction)buttonPressed:(id)sender {
if (sender == portraitFooButton || sender == landscapeFooButton) {
portraitFooButton.hidden = YES;
landscapeFooButton.hidden = YES;
}
else {
portraitBarButton.hidden = YES;
landscapeBarButton.hidden = YES;
}
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)
interfaceOrientation duration:(NSTimeInterval)duration {
if (interfaceOrientation == UIInterfaceOrientationPortrait)
{
self.view = self.portrait;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(0));
self.view.bounds = CGRectMake(0.0, 0.0, 300.0, 480.0);
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
{
self.view = self.landscape;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(-90));
self.view.bounds = CGRectMake(0.0, 0.0, 460.0, 320.0);
}
else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
self.view = self.portrait;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(180));
self.view.bounds = CGRectMake(0.0, 0.0, 300.0, 480.0);
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
self.view = self.landscape;
self.view.transform = CGAffineTransformIdentity;
self.view.transform =
CGAffineTransformMakeRotation(degreesToRadian(90));
self.view.bounds = CGRectMake(0.0, 0.0, 460.0, 320.0);
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
self.landscape = nil;
self.portrait = nil;
self.landscapeFooButton = nil;
self.landscapeBarButton = nil;
self.portraitFooButton = nil;
self.portraitBarButton = nil;
[super viewDidUnload];
}
- (void)dealloc {
[landscape release];
[portrait release];
[landscapeFooButton release];
[portraitFooButton release];
[landscapeBarButton release];
[portraitBarButton release];
[super dealloc];
}
设计两个视图
我们不想使用作为模板一部分的现有视图,因为它的大小无法改变,于是删除默认视图,创建两个新的视图。
从库中拖出两个View放到主窗口中,分别命名为Portrait和Landscape。
按住Control键并将Files Owner图标拖到Portrait图标,在弹出灰色菜单时选择portrait;然后,再次按住Control键并将Files Owner图标拖到Landscape图标,在弹出灰色菜单时选择landscape。现在,按住Control键并将Files Owner图标拖到Portrait图标,在弹出灰色菜单时选择view。指定启动时显示的视图。
@end