码迷,mamicode.com
首页 > 其他好文 > 详细

UIViewController新方法的使用(transitionFromViewController:toViewController:duration:options:animations:completion:)

时间:2014-05-27 16:07:35      阅读:456      评论:0      收藏:0      [点我收藏+]

标签:style   c   class   blog   code   java   

iOS5中,UIViewController新添加了几个方法:

bubuko.com,布布扣
- (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);
bubuko.com,布布扣

代码示例:

代码结构:

bubuko.com,布布扣

其中,MainViewController添加为window的rootViewController,FirstViewController、SecondViewController、ThirdViewController就是三个简单的controller,设置不同的背景色即可,MainViewController的xib文件如下图:

bubuko.com,布布扣

MainViewController的viewDidLoad方法实现如下:

bubuko.com,布布扣
- (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;
}
bubuko.com,布布扣

三个button的响应方法如下:

bubuko.com,布布扣
- (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;
    }
}
bubuko.com,布布扣

 

效果图:

bubuko.com,布布扣   bubuko.com,布布扣  bubuko.com,布布扣

UIViewController新方法的使用(transitionFromViewController:toViewController:duration:options:animations:completion:),布布扣,bubuko.com

UIViewController新方法的使用(transitionFromViewController:toViewController:duration:options:animations:completion:)

标签:style   c   class   blog   code   java   

原文地址:http://www.cnblogs.com/benbenzhu/p/3753112.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!