【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"];
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值