使用动画隐藏导航控制器和标签栏控制器

时间:2012-10-25 15:16:15

标签: ios uinavigationcontroller uitabbarcontroller uiviewanimation

我正试图在触摸按钮时同时隐藏UITabBarController和UINavigationController。我在这里找到了一个非常好的代码片段How to hide uitabbarcontroller但是在尝试隐藏和动画UINavigationController和tabbarcontroller时我遇到了问题。我还在互联网上发现了许多使用self.tabBarController.tabBar.hidden = YES隐藏标签栏但是只隐藏按钮项而不是底部黑条的示例。

在玩了很多之后,我可以正确制作两个动画,因为我认为它与导航控制器的隐藏有关,这使得整个窗口的大小可以随时改变。

-(IBAction)touchImage:(id)sender {

    if (isImageFullScreen) {

        isImageFullScreen = NO;

        [self.navigationController setNavigationBarHidden:NO animated:YES];
        [UIView transitionWithView:self.view
                          duration:0.5
                           options:UIViewAnimationOptionCurveLinear
                        animations:^
         {
             hotelImageButton.frame = CGRectMake(0,20,320,92);
             [self showTabBar:self.tabBarController];
         }
                        completion:^(BOOL finished)
         {
         }];

    } else {

        isImageFullScreen = YES;

        [self.navigationController setNavigationBarHidden:YES animated:YES];
        [UIView transitionWithView:self.view
                          duration:0.5
                           options:UIViewAnimationOptionCurveLinear
                        animations:^
         {
             hotelImageButton.frame = CGRectMake(0,0,320,480);
             [self hideTabBar:self.tabBarController];
         }
                        completion:^(BOOL finished)
         {                                  
         }];
    }

}

hideTabBar和showTabBar方法来自我上面链接的其他帖子。

我也尝试过其他一些组合,但我不能让它看起来不错。有什么想法吗?

提前致谢。

2 个答案:

答案 0 :(得分:5)

我现在尝试了这段代码,我发现UITabBar节目动画不能顺利进行。 我设法通过调整显示动画的标签栏的持续时间来使其更平滑。

[UIView setAnimationDuration:0.2];

希望这有效。

编辑: 请尝试此代码,它会调整父视图的大小,使其在1个动画事务中更大,只有隐藏条形并显示内容。

- (IBAction)TestButton1:(UIButton *)sender {

if(!isAnimating){
    if(isTabBarAndNavBarHidden){

        [UIView transitionWithView:self.view
                          duration:0.5
                           options:UIViewAnimationOptionTransitionNone
                        animations:^
         {
             isAnimating=YES;

             CGFloat statusBar_height=[[UIApplication sharedApplication] statusBarFrame].size.height;
             CGFloat screen_height=[UIScreen mainScreen].bounds.size.height;

             [self.tabBarController.view setFrame:CGRectMake(self.tabBarController.view.frame.origin.x, 0, self.tabBarController.view.frame.size.width, screen_height)];
             [self.navigationController.navigationBar setFrame:CGRectMake(self.navigationController.navigationBar.frame.origin.x, statusBar_height, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
         }
                        completion:^(BOOL finished)
         {
             isTabBarAndNavBarHidden=NO;
             isAnimating=NO;
         }];

    }else{

        [UIView transitionWithView:self.view
                          duration:0.5
                           options:UIViewAnimationOptionTransitionNone
                        animations:^
         {
             isAnimating=YES;

             CGFloat statusBar_height=[[UIApplication sharedApplication] statusBarFrame].size.height;
             CGFloat screen_height=[UIScreen mainScreen].bounds.size.height;

             [self.tabBarController.view setFrame:CGRectMake(self.tabBarController.view.frame.origin.x, statusBar_height-self.navigationController.navigationBar.frame.size.height, self.tabBarController.view.frame.size.width, screen_height+self.navigationController.navigationBar.frame.size.height+self.tabBarController.tabBar.frame.size.height-statusBar_height)];
             [self.navigationController.navigationBar setFrame:CGRectMake(self.navigationController.navigationBar.frame.origin.x, 0, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];


         }
                        completion:^(BOOL finished)
         {
             isTabBarAndNavBarHidden=YES;
             isAnimating=NO;
         }];

    }
}

}

答案 1 :(得分:0)

此代码适用于iPhone 4 / 4S。

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.lastContentOffset > scrollView.contentOffset.y)
    {
          NSLog(@"Scrolling up");

        [UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{



            [self.tabBarController.tabBar setFrame:CGRectMake(0, 430, 320, 50)];
             [self.navigationController.navigationBar setFrame:CGRectMake(0, 20, self.navigationController.navigationBar.frame.size.width,self.navigationController.navigationBar.frame.size.height)];

             } completion:
           ^(BOOL finished) {

                [UIView animateWithDuration:.5 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{


                      } completion:^(BOOL finished) {
                              //
                        }];

            }];

    }
    else if (self.lastContentOffset < scrollView.contentOffset.y)
    {
        [UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
               [self.navigationController.navigationBar setFrame:CGRectMake(0, -60, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
            [self.tabBarController.tabBar setFrame:CGRectMake(0, 480, 320, 50)];


        } completion:
         ^(BOOL finished) {

             [UIView animateWithDuration:.5 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{

             } completion:^(BOOL finished) {

             }];

         }];



        NSLog(@"Scrolling Down");

    }

    self.lastContentOffset = scrollView.contentOffset.y;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.tabBarController.tabBar setFrame:CGRectMake(0, 430, 320, 50)];
     [self.navigationController.navigationBar setFrame:CGRectMake(0, 20, self.navigationController.navigationBar.frame.size.width,self.navigationController.navigationBar.frame.size.height)];



    // Do any additional setup after loading the view.
}
相关问题