iOS仿網(wǎng)易新聞之類的滑動(dòng)標(biāo)題欄,供大家參考,具體內(nèi)容如下
創(chuàng)新互聯(lián)建站是一家專注于網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作與策劃設(shè)計(jì),古田網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:古田等地區(qū)。古田做網(wǎng)站價(jià)格咨詢:18980820575
預(yù)覽
思路
兩個(gè)scorllview,一個(gè)用于標(biāo)題欄,一個(gè)擁有底下的page
標(biāo)題欄文字和效果切換,漸變色和大小都是根據(jù)底下的page偏移量來(lái)歸一化換算的
小橫線直接加載標(biāo)題欄所在的scorllview里面,小橫線自身要有局部偏移,根據(jù)page來(lái)切換
標(biāo)題欄的居中需要算一個(gè)scrollview的偏移量,小橫線跟著scorllview偏移
監(jiān)聽scrollview的滑動(dòng)和停止滑動(dòng)進(jìn)行相應(yīng)的處理
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self createTitleScrollView]; [self createButtonLine]; [self createContentScrollview]; } #pragma mark - 初始化UI - (void)createTitleScrollView { // 根據(jù)是否有導(dǎo)航欄調(diào)整坐標(biāo) CGFloat marginY = self.navigationController ? self.navigationController.view.frame.size.height : kUpMargin; // 標(biāo)題欄,包括小橫線的位置 _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 = @[@"頭條", @"社會(huì)", @"財(cái)經(jīng)", @"科技", @"體育", @"娛樂", @"時(shí)尚", @"軍事", @"教育", @"游戲"]; _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做標(biāo)記,方便后面索引,為了不出沖突,就把這個(gè)數(shù)值設(shè)得大一些 [_titleScrollView addSubview:titleBtn]; }; } - (void)createButtonLine { // 初始時(shí)刻停在最左邊與按鈕對(duì)齊 _buttonLine = [[UIView alloc] initWithFrame:CGRectMake(0, kTitleHeight, kButtonWidth, kButtonLineHeight)]; _buttonLine.backgroundColor = [UIColor redColor]; // 小橫線加載scrollview上才能跟隨button聯(lián)動(dòng) [_titleScrollView addSubview:_buttonLine]; } - (void)createContentScrollview { CGFloat marginY = self.navigationController ? self.navigationController.view.frame.size.height : kUpMargin; // 添加內(nèi)容頁(yè)面 _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]; // 添加分頁(yè)面 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]; } // 初始化后選中某個(gè)欄目 [self titleButtonClicked:[_titleScrollView viewWithTag:1000 + 0]]; } #pragma mark - 標(biāo)題button點(diǎn)擊事件 - (void)titleButtonClicked:(UIButton *)sender { // 根據(jù)點(diǎn)擊的button切換頁(yè)面和偏移 printf("%s clicked\n", sender.currentTitle.UTF8String); // 以下不用了,因?yàn)閟croll切換會(huì)自動(dòng)處理好尺寸和顏色了 // 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]; // } // 強(qiáng)調(diào)被選中的button // 放大聚焦 sender.transform = CGAffineTransformMakeScale(kMaxTitleScale, kMaxTitleScale); // 變色 [sender setTitleColor:[UIColor greenColor] forState:UIControlStateNormal]; // 居中title [self settleTitleButton:sender]; // 帶動(dòng)畫切換到對(duì)應(yīng)的內(nèi)容,會(huì)觸發(fā)scrollViewDidScroll NSInteger pageIndex = sender.tag - 1000; [_contentScrollview setContentOffset:CGPointMake(kFrameWidth * pageIndex, 0) animated:YES]; } #pragma mark - scrollview滑動(dòng)事件 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { // 根據(jù)content內(nèi)容偏移調(diào)整標(biāo)題欄 if (scrollView == _titleScrollView) { printf("title moved\n"); } else if (scrollView == _contentScrollview) { printf("content moved\n"); // 獲得左右兩個(gè)button的索引, 注意最后取整 CGFloat offsetX = scrollView.contentOffset.x; NSInteger leftTitleIndex = offsetX / kFrameWidth; NSInteger rightTitleIndex = leftTitleIndex + 1; // 因?yàn)樵O(shè)置了到邊不能滑動(dòng),所以不考慮邊界 UIButton *leftTitleButton = [_titleScrollView viewWithTag:1000 + leftTitleIndex]; UIButton *rightTitleButton = [_titleScrollView viewWithTag:1000 + rightTitleIndex]; // 設(shè)置大小和顏色漸變以及小橫線的聯(lián)動(dòng) // 權(quán)重因子 0~1 小數(shù), 左邊和右邊互補(bǔ) 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ū)塊滑動(dòng)結(jié)束調(diào)整標(biāo)題欄居中 if (scrollView == _contentScrollview) { // 取得索引值 NSInteger titleIndex = scrollView.contentOffset.x / kFrameWidth; // title居中 [self settleTitleButton:[_titleScrollView viewWithTag:1000 + titleIndex]]; } } #pragma mark - 標(biāo)題按鈕和橫線居中偏移 - (void)settleTitleButton:(UIButton *)button { // 標(biāo)題 // 這個(gè)偏移量是相對(duì)于scrollview的content frame原點(diǎn)的相對(duì)對(duì)標(biāo) CGFloat deltaX = button.center.x - kFrameWidth / 2; // 設(shè)置偏移量,記住這段算法 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效果
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。