【iOS开发】---- 表格滚动时隐藏及显示导航条和标签栏

在iOS开发中,以瀑布流浏览图片时通常希望能更多空间来展示内容,这样我们就希望UIScrollView滚动时隐藏及显示导航条和标签栏。

我们希望向下滚动时显示,向上滚动时隐藏,同时希望隐藏和显示的动画能够流畅一点。这样的话,我们需要做到以下几点:

  • 判断是向上还是向下滚动
  • 隐藏和显示导航标签栏时有流畅的动画
实现的代码如下:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    static float lastOffY  = 0;
    float curOffY = scrollView.contentOffset.y;

    if (scrollView.frame.size.height >= scrollView.contentSize.height ||     //内容高度低于scrollView高度,不隐藏
        fabs(curOffY)  +SCREEN_SIZE_HEIGHT> scrollView.contentSize.height || //拉至最底部时,不做处理
        curOffY < 0                                                          //拉至最顶部时,不做处理
        )
    {
        return;
    }
    if (curOffY - lastOffY > 40)
    {
        //向上
        lastOffY = curOffY;
       [self hideTabBar];
      
    }
    else if(lastOffY -curOffY >40)
    {
        //向下
        lastOffY = curOffY;
       [self showTabBar];
    }
}

上面的代码有判断向上还是向下的方法,向上或向下滚动40个高度后引发显示或收藏的动作,我之所以选40(这个随意),是觉得不大不小,效果还不错,还能避免顶部或底部反弹时引发的其它问题。
下面是实现隐藏的和显示的代码:
- (void)showTabBar
{
    if (self.tabBarController.tabBar.hidden == NO)
    {
        return;
    }
    
    UIView *contentView;
    if ([[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]])
        contentView = [self.tabBarController.view.subviews objectAtIndex:1];
    else
        contentView = [self.tabBarController.view.subviews objectAtIndex:0];
    
    contentView.frame = CGRectMake(contentView.bounds.origin.x,
                                   contentView.bounds.origin.y,
                                   contentView.bounds.size.width,
                                   contentView.bounds.size.height - self.tabBarController.tabBar.frame.size.height);
    
    CATransition *animation = [CATransition animation];
    animation.duration = 0.4f;
    animation.type = kCATransitionMoveIn;
    animation.subtype = kCATransitionFromTop;
    self.tabBarController.tabBar.hidden = NO;
  	[self.tabBarController.tabBar.layer addAnimation:animation forKey:@"animation2"];
    
    CATransition *animation1 = [CATransition animation];
    animation1.duration = 0.4f;
    animation1.type = kCATransitionMoveIn;
    animation1.subtype = kCATransitionFromBottom;
    self.navigationController.navigationBarHidden = NO;
  	[self.navigationController.navigationBar.layer addAnimation:animation1 forKey:@"animation3"];
   
}

- (void)hideTabBar
{
    if (self.tabBarController.tabBar.hidden == YES)
    {
        return;
    }
    UIView *contentView;
    if ( [[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]] )
        contentView = [self.tabBarController.view.subviews objectAtIndex:1];
    else
        contentView = [self.tabBarController.view.subviews objectAtIndex:0];
    contentView.frame = CGRectMake(contentView.bounds.origin.x,
                                   contentView.bounds.origin.y,
                                   contentView.bounds.size.width,
                                   contentView.bounds.size.height + self.tabBarController.tabBar.frame.size.height);

    
    CATransition *animation1 = [CATransition animation];
    animation1.timingFunction=UIViewAnimationCurveEaseInOut;
    animation1.duration = 0.4f;
    animation1.delegate =self;
    animation1.type = kCATransitionReveal;
    animation1.subtype = kCATransitionFromTop;
    self.navigationController.navigationBarHidden = YES;
  	[self.navigationController.navigationBar.layer addAnimation:animation1 forKey:@"animation0"];
    
    //定义个转场动画
    CATransition *animation = [CATransition animation];
    //转场动画持续时间
    animation.duration = 0.4f;
    //计时函数,从头到尾的流畅度???
    animation.timingFunction=UIViewAnimationCurveEaseInOut;
    //转场动画类型
    animation.type = kCATransitionReveal;
    //转场动画子类型
    animation.subtype = kCATransitionFromBottom;
    //动画时你需要的实现
    self.tabBarController.tabBar.hidden = YES;
      //添加动画 (转场动画是添加在层上的动画)
  	[self.tabBarController.tabBar.layer addAnimation:animation forKey:@"animation1"];
}


你可能感兴趣的:(【iOS开发】)