首页 > 代码库 > UIViewController新方法的使用(transitionFromViewController:toViewController:duration:options:animations:completion:)
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; } }
效果图:
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。