• 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;
        }
    }

    效果图:

         

  • 相关阅读:
    Hadoop组成
    Less学习笔记
    Java笔记二:面向对象基础
    对接腾讯云及时通讯总结
    MySQL主从模式及配置
    ~/sdat2img.py system.transfer.list system.new.dat system.img
    获取安卓官方原厂Recovery
    安卓安装 adb & fastboot
    linux简单配置nginx并实现https转发
    linux部署禅道并使用自定义mysql数据库
  • 原文地址:https://www.cnblogs.com/benbenzhu/p/3753112.html
Copyright © 2020-2023  润新知