【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;

}

转载出处: 点击打开链接

你可能感兴趣的:(ios,iPhone,hierarchy)