#import "RootViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface RootViewController ()
@property (nonatomic, retain) SecondViewController *secondVC;
@property (nonatomic, retain) ThirdViewController *thirdVC;
@property (nonatomic, assign) BOOL isShowSecondView;
@end
@implementation RootViewController
- (void)dealloc
{
[_secondVC release];
[_thirdVC release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor greenColor];
[self addChildViews];
[self addButtonItem];
}
// 添加控制器显示视图
- (void)addChildViews
{
// 创建控制器
self.thirdVC = [[ThirdViewController alloc] init];
// 添加子控制器
[self addChildViewController:self.thirdVC];
// 显示thirdVC.View
[self.view addSubview:self.thirdVC.view];
[_thirdVC release];
// 创建控制器
self.secondVC = [[SecondViewController alloc] init];
// 把该控制器添加成rootVC的自控制器
// 把secondVC.View显示出来
[self addChildViewController:self.secondVC];
[self.view addSubview:self.secondVC.view];
[_secondVC release];
// 给标识一个初值
self.isShowSecondView = YES;
}
// 添加按钮
- (void)addButtonItem
{
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"切换" style:UIBarButtonItemStyleDone target:self action:@selector(actionButton:)];
self.navigationItem.leftBarButtonItem = button;
}
// 实现切换视图
- (void)actionButton:(UIBarButtonItem *)button
{
if (self.isShowSecondView == YES) {
// UIView 切换的动画
[UIView transitionFromView:self.secondVC.view toView:self.thirdVC.view duration:1 options:UIViewAnimationOptionTransitionFlipFromLeft completion:nil];
} else {
// UIView 切换的动画
[UIView transitionFromView:self.thirdVC.view toView:self.secondVC.view duration:1 options:UIViewAnimationOptionTransitionFlipFromRight completion:nil];
}
self.isShowSecondView = !self.isShowSecondView;
}
切换视图动画
最新推荐文章于 2024-11-08 11:06:20 发布