天天看點

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

繼續閱讀