溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

iOS仿新聞tab標題欄效果

發(fā)布時間:2020-08-22 04:34:59 來源:腳本之家 閱讀:238 作者:踏莎行hyx 欄目:移動開發(fā)

iOS仿網(wǎng)易新聞之類的滑動標題欄,供大家參考,具體內(nèi)容如下

預覽

iOS仿新聞tab標題欄效果

思路

兩個scorllview,一個用于標題欄,一個擁有底下的page
標題欄文字和效果切換,漸變色和大小都是根據(jù)底下的page偏移量來歸一化換算的
小橫線直接加載標題欄所在的scorllview里面,小橫線自身要有局部偏移,根據(jù)page來切換
標題欄的居中需要算一個scrollview的偏移量,小橫線跟著scorllview偏移
監(jiān)聽scrollview的滑動和停止滑動進行相應的處理

- (void)viewDidLoad
{
 [super viewDidLoad];
 // Do any additional setup after loading the view.
 [self createTitleScrollView];
 [self createButtonLine];
 [self createContentScrollview];
}
 
#pragma mark - 初始化UI
- (void)createTitleScrollView
{
 // 根據(jù)是否有導航欄調(diào)整坐標
 CGFloat marginY = self.navigationController ? self.navigationController.view.frame.size.height : kUpMargin;
 
 // 標題欄,包括小橫線的位置
 _titleScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, marginY, kFrameWidth, kTitleHeight + kButtonLineHeight)];
 _titleScrollView.showsHorizontalScrollIndicator = NO;
 _titleScrollView.bounces = NO;
 _titleScrollView.delegate = self;
 [self.view addSubview:_titleScrollView];
 
 // 添加button
 NSArray *titleArray = @[@"頭條", @"社會", @"財經(jīng)", @"科技", @"體育", @"娛樂", @"時尚", @"軍事", @"教育", @"游戲"];
 _pageCount = titleArray.count;
 _titleScrollView.contentSize = CGSizeMake(kButtonWidth * _pageCount, kTitleHeight);
 for (int i = 0; i < _pageCount; i++)
 {
  UIButton *titleBtn = [[UIButton alloc] initWithFrame:CGRectMake(kButtonWidth * i, 0, kButtonWidth, kTitleHeight)];
  [titleBtn setTitle:titleArray[i] forState:UIControlStateNormal];
  [titleBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  titleBtn.titleLabel.font = [UIFont systemFontOfSize:16];
  [titleBtn addTarget:self action:@selector(titleButtonClicked:) forControlEvents:UIControlEventTouchDown];
  titleBtn.tag = 1000 + i; // button做標記,方便后面索引,為了不出沖突,就把這個數(shù)值設得大一些
  [_titleScrollView addSubview:titleBtn];
 };
}
 
- (void)createButtonLine
{
 // 初始時刻停在最左邊與按鈕對齊
 _buttonLine = [[UIView alloc] initWithFrame:CGRectMake(0, kTitleHeight, kButtonWidth, kButtonLineHeight)];
 _buttonLine.backgroundColor = [UIColor redColor];
 // 小橫線加載scrollview上才能跟隨button聯(lián)動
 [_titleScrollView addSubview:_buttonLine];
}
 
- (void)createContentScrollview
{
 CGFloat marginY = self.navigationController ? self.navigationController.view.frame.size.height : kUpMargin;
 
 // 添加內(nèi)容頁面
 _contentScrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, marginY + kTitleHeight + kButtonLineHeight, kFrameWidth, kFrameHeight - marginY - kTitleHeight - kButtonLineHeight)];
 _contentScrollview.pagingEnabled = YES;
 _contentScrollview.bounces = NO;
 _contentScrollview.contentSize = CGSizeMake(kFrameWidth * _pageCount, kFrameHeight - marginY - kTitleHeight);
 _contentScrollview.showsHorizontalScrollIndicator = NO;
 _contentScrollview.delegate = self;
 [self.view addSubview:_contentScrollview];
 
 // 添加分頁面
 for (int i = 0; i < _pageCount; i++)
 {
  PageViewController *pageViewController = [[PageViewController alloc] init];
  UIButton *button = [_titleScrollView viewWithTag:1000 + i];
  pageViewController.title = button.currentTitle;
  pageViewController.view.frame = CGRectMake(kFrameWidth * i, 0, kFrameWidth, kFrameHeight - marginY - kTitleHeight);
  [_contentScrollview addSubview:pageViewController.view];
 }
 
 // 初始化后選中某個欄目
 [self titleButtonClicked:[_titleScrollView viewWithTag:1000 + 0]];
}
 
#pragma mark - 標題button點擊事件
- (void)titleButtonClicked:(UIButton *)sender
{
 // 根據(jù)點擊的button切換頁面和偏移
 printf("%s clicked\n", sender.currentTitle.UTF8String);
 
 // 以下不用了,因為scroll切換會自動處理好尺寸和顏色了
// for (int i = 0; i < _pageCount; i++)
// {
//  UIButton *button = [_titleScrollView viewWithTag:1000 + i];
//  // 重置button尺寸顏色
//  button.transform = CGAffineTransformMakeScale(1, 1);
//  [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// }
 
 // 強調(diào)被選中的button
 // 放大聚焦
 sender.transform = CGAffineTransformMakeScale(kMaxTitleScale, kMaxTitleScale);
 // 變色
 [sender setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
 
 // 居中title
 [self settleTitleButton:sender];
 
 // 帶動畫切換到對應的內(nèi)容,會觸發(fā)scrollViewDidScroll
 NSInteger pageIndex = sender.tag - 1000;
 [_contentScrollview setContentOffset:CGPointMake(kFrameWidth * pageIndex, 0) animated:YES];
}
 
#pragma mark - scrollview滑動事件
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
 // 根據(jù)content內(nèi)容偏移調(diào)整標題欄
 if (scrollView == _titleScrollView)
 {
  printf("title moved\n");
  
  
 }
 else if (scrollView == _contentScrollview)
 {
  printf("content moved\n");
  
  // 獲得左右兩個button的索引, 注意最后取整
  CGFloat offsetX = scrollView.contentOffset.x;
  NSInteger leftTitleIndex = offsetX / kFrameWidth;
  NSInteger rightTitleIndex = leftTitleIndex + 1;
  // 因為設置了到邊不能滑動,所以不考慮邊界
  UIButton *leftTitleButton = [_titleScrollView viewWithTag:1000 + leftTitleIndex];
  UIButton *rightTitleButton = [_titleScrollView viewWithTag:1000 + rightTitleIndex];
  
  // 設置大小和顏色漸變以及小橫線的聯(lián)動
  // 權(quán)重因子 0~1 小數(shù), 左邊和右邊互補
  CGFloat rightTitleFactor = offsetX / kFrameWidth - leftTitleIndex;
  CGFloat leftTitleFactor = 1 - rightTitleFactor;
  
  // 尺寸
  CGFloat maxExtraScale = kMaxTitleScale - 1;
  leftTitleButton.transform = CGAffineTransformMakeScale(1 + leftTitleFactor * maxExtraScale, 1 + leftTitleFactor * maxExtraScale);
  rightTitleButton.transform = CGAffineTransformMakeScale(1 + rightTitleFactor * maxExtraScale, 1 + rightTitleFactor * maxExtraScale);
  // 顏色
  UIColor *leftTitleColor = [UIColor colorWithRed:0 green:leftTitleFactor blue:0 alpha:1];
  UIColor *rightTitleColor = [UIColor colorWithRed:0 green:rightTitleFactor blue:0 alpha:1];
  [leftTitleButton setTitleColor:leftTitleColor forState:UIControlStateNormal];
  [rightTitleButton setTitleColor:rightTitleColor forState:UIControlStateNormal];
  // 小橫線位移
  _buttonLine.frame = CGRectMake(kButtonWidth * (leftTitleIndex + rightTitleFactor), _buttonLine.frame.origin.y, kButtonWidth, kButtonLineHeight);
 }
}
 
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
 // 內(nèi)容區(qū)塊滑動結(jié)束調(diào)整標題欄居中
 if (scrollView == _contentScrollview)
 {
  // 取得索引值
  NSInteger titleIndex = scrollView.contentOffset.x / kFrameWidth;
  // title居中
  [self settleTitleButton:[_titleScrollView viewWithTag:1000 + titleIndex]];
 }
}
 
#pragma mark - 標題按鈕和橫線居中偏移
- (void)settleTitleButton:(UIButton *)button
{
 // 標題
 // 這個偏移量是相對于scrollview的content frame原點的相對對標
 CGFloat deltaX = button.center.x - kFrameWidth / 2;
 // 設置偏移量,記住這段算法
 if (deltaX < 0)
 {
  // 最左邊
  deltaX = 0;
 }
 CGFloat maxDeltaX = _titleScrollView.contentSize.width - kFrameWidth;
 if (deltaX > maxDeltaX)
 {
  // 最右邊不能超范圍
  deltaX = maxDeltaX;
 }
 [_titleScrollView setContentOffset:CGPointMake(deltaX, 0) animated:YES];
 
}

源代碼下載

csdn:仿網(wǎng)易新聞tab效果

github:仿網(wǎng)易新聞tab效果

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI