[IOS]iphone开发之横屏与竖屏在不同视图之间的切换
有两个视图,横屏视图和纵屏视图,当iphone的方位变化的时候,这两个视图相互切换。
1。两个视图:PortraitView和LandscapeView ,分别标示纵屏和横屏。
2。一个控制器,RootViewController,根控制器。
3。在RootViewController.m中有以下代码
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
[landscape removeFromSuperview];
[self.view addSubview:portrait];
}
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
[portrait removeFromSuperview];
[self.view addSubview:landscape];
}
}
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
UIControl *back = [[UIControl alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
back.backgroundColor = [UIColor grayColor];
self.view = back;
[back release];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
portrait = [[PortraitView alloc] initWithFrame:CGRectMake(10, 10, 300, 440)];
portrait.backgroundColor = [UIColor yellowColor];
[portrait addButton];
landscape = [[LandscapeView alloc] initWithFrame:CGRectMake(10, 10, 460, 280)];
landscape.backgroundColor = [UIColor greenColor];
[self.view addSubview:portrait];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return YES;
}
分析:willAnimateRotationToInterfaceOrientation方法是用于横纵屏变化的时候的切换用。
portrait和landscape是两个视图。