UIViewController新方法的使用(transitionFromViewController:toViewController:duration:options:animations:completion:)
iOS5中,UIViewController新添加了几个方法:
- (void)addChildViewController:(UIViewController *)childController NS_AVAILABLE_IOS(5_0); - (void) removeFromParentViewController NS_AVAILABLE_IOS(5_0); - (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(5_0); - (void)willMoveToParentViewController:(UIViewController *)parent NS_AVAILABLE_IOS(5_0); - (void)didMoveToParentViewController:(UIViewController *)parent NS_AVAILABLE_IOS(5_0);
代码示例:
代码结构:
其中,MainViewController添加为window的rootViewController,FirstViewController、SecondViewController、ThirdViewController就是三个简单的controller,设置不同的背景色即可,MainViewController的xib文件如下图:
MainViewController的viewDidLoad方法实现如下:
- (void)viewDidLoad { [super viewDidLoad]; FirstViewController * firstController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil]; [self addChildViewController:firstController]; SecondViewController * secondController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; [self addChildViewController:secondController]; ThirdViewController * thirdController = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil]; [self addChildViewController:thirdController]; [contentView addSubview:firstController.view];//用于显示其它controller的view currentController = firstController; }
三个button的响应方法如下:
- (IBAction)onButtonClick:(id)sender { FirstViewController * firstController = self.childViewControllers[0]; SecondViewController * secondController = self.childViewControllers[1]; ThirdViewController * thirdController = self.childViewControllers[2]; NSInteger tag = ((UIButton *) sender).tag; if ((currentController == firstController && tag == 1) || (currentController == secondController && tag == 2) || (currentController == thirdController && tag == 3)) { return; } UIViewController * oldController = currentController; switch (tag) { case 1:{ [self transitionFromViewController:currentController toViewController:firstController duration:1 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{} completion:^(BOOL finished) { if (finished) { currentController = firstController; } else { currentController = oldController; } }]; break; } case 2: { [self transitionFromViewController:currentController toViewController:secondController duration:1 options:UIViewAnimationOptionTransitionFlipFromTop animations:^{} completion:^(BOOL finished) { if (finished) { currentController = secondController; } else { currentController = oldController; } }]; break; } case 3:{ [self transitionFromViewController:currentController toViewController:thirdController duration:1 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{} completion:^(BOOL finished) { if (finished) { currentController = thirdController; } else { currentController = oldController; } }]; break; } default: break; } }
效果图: