【iOS開發】---- 表格滾動時隱藏及顯示導航條和標籤欄
阿新 • • 發佈:2019-02-05
在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"]; }