zoukankan      html  css  js  c++  java
  • 动画隐藏UITabBarController与UINavigationController

    动画隐藏UITabBarController与UINavigationController

    效果图:

    源码:

    AppDelegate.m

    //
    //  AppDelegate.m
    //  HideTabbar
    //
    //  Copyright (c) 2014年 Y.X. All rights reserved.
    //
    
    #import "AppDelegate.h"
    #import "RootViewController.h"
    
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        UITabBarController *tab        = [[UITabBarController alloc] init];
        tab.viewControllers            = @[[RootViewController new]];
        UITabBar     *tabBar           = tab.tabBar;
        UITabBarItem *tabBarItem       = [tabBar.items objectAtIndex:0];
        tabBarItem.title               = @"YouXianMing";
        NSDictionary *textDic          = 
            @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Thin"
                                                   size:20.f]};
        [tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -12.f)];
        [tabBarItem setTitleTextAttributes:textDic
                                  forState:UIControlStateNormal];
        
        UINavigationController *NC     = 
            [[UINavigationController alloc] initWithRootViewController:tab];
        self.window.rootViewController = NC;
        self.window.backgroundColor    = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
        return YES;
    }
    
    @end

    RootViewController.m

    //
    //  RootViewController.m
    //  HideTabbar
    //
    //  Copyright (c) 2014年 Y.X. All rights reserved.
    //
    
    #import "RootViewController.h"
    
    @interface RootViewController ()
    
    @property (nonatomic, assign) BOOL      flag;
    
    @end
    
    @implementation RootViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.view.layer.contents    = (__bridge id)([UIImage imageNamed:@"back"].CGImage);
    
        // 添加手势
        UITapGestureRecognizer *tap = 
            [[UITapGestureRecognizer alloc] initWithTarget:self
                                                    action:@selector(event:)];
        [self.view addGestureRecognizer:tap];
    }
    
    - (void)event:(UITapGestureRecognizer *)tap
    {
        if (!_flag)
        {
            [self hideTabBar:self.tabBarController];
        }
        else
        {
            [self showTabBar:self.tabBarController];
        }
        
        _flag = !_flag;
    }
    
    - (void)hideTabBar:(UITabBarController *)tabbarcontroller
    {
        // 隐藏导航栏
        [self.navigationController setNavigationBarHidden:YES animated:YES];
        
        // 隐藏tabbar
        [UIView animateWithDuration:UINavigationControllerHideShowBarDuration animations:^{
            for(UIView *view in tabbarcontroller.view.subviews)
            {
                if([view isKindOfClass:[UITabBar class]])
                {
                    [view setFrame:CGRectMake(view.frame.origin.x,
                                              view.frame.origin.y + 50,
                                              view.frame.size.width,
                                              view.frame.size.height)];
                }
                else
                {
                    [view setFrame:CGRectMake(view.frame.origin.x,
                                              view.frame.origin.y,
                                              view.frame.size.width,
                                              view.frame.size.height + 50)];
                }
            }
        }];
    }
    
    - (void)showTabBar:(UITabBarController *)tabbarcontroller
    {    
        // 显示导航栏
        [self.navigationController setNavigationBarHidden:NO animated:YES];
        
        // 显示tabbar
        [UIView animateWithDuration:UINavigationControllerHideShowBarDuration animations:^{
            for(UIView *view in tabbarcontroller.view.subviews)
            {
                if([view isKindOfClass:[UITabBar class]])
                {
                    [view setFrame:CGRectMake(view.frame.origin.x,
                                              view.frame.origin.y - 50,
                                              view.frame.size.width,
                                              view.frame.size.height)];
                }
                else
                {
                    [view setFrame:CGRectMake(view.frame.origin.x,
                                              view.frame.origin.y,
                                              view.frame.size.width,
                                              view.frame.size.height - 50)];
                }
            }
        }];
    }
    
    @end

    核心的地方:

  • 相关阅读:
    Mayan游戏 (codevs 1136)题解
    虫食算 (codevs 1064)题解
    靶形数独 (codevs 1174)题解
    黑白棋游戏 (codevs 2743)题解
    神经网络 (codevs 1088) 题解
    The Rotation Game (POJ 2286) 题解
    倒水问题 (codevs 1226) 题解
    银河英雄传说 (codevs 1540) 题解
    生日蛋糕 (codevs 1710) 题解
    第一章 1.11 高阶函数
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3829085.html
Copyright © 2011-2022 走看看