zoukankan      html  css  js  c++  java
  • 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;
        }
    }

    效果图:

         

  • 相关阅读:
    LRESULT CALLBACK WndProc 窗口程序的 重点
    vs2012打开低版本项目时 出现vs2012警告未能加载包“visual c++ package 解决办法
    const char*, char const*, char*const的区别
    js手机短信按钮倒计时
    控件案例
    laydate.js时间选择
    jquery.reveal弹出框
    表格内容控制(换行、不换行、隐藏)
    sql 把特定数据排在最前面
    jquery选择器(可见对象,不可见对象) +判断,对象(逆序)
  • 原文地址:https://www.cnblogs.com/benbenzhu/p/3753112.html
Copyright © 2011-2022 走看看