iOS仿網(wǎng)易新聞之類的滑動標(biāo)題欄,供大家參考,具體內(nèi)容如下
10年積累的網(wǎng)站設(shè)計制作、做網(wǎng)站經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認識你,你也不認識我。但先建設(shè)網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有荔波免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。預(yù)覽
思路
兩個scorllview,一個用于標(biāo)題欄,一個擁有底下的page
標(biāo)題欄文字和效果切換,漸變色和大小都是根據(jù)底下的page偏移量來歸一化換算的
小橫線直接加載標(biāo)題欄所在的scorllview里面,小橫線自身要有局部偏移,根據(jù)page來切換
標(biāo)題欄的居中需要算一個scrollview的偏移量,小橫線跟著scorllview偏移
監(jiān)聽scrollview的滑動和停止滑動進行相應(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 = @[@"頭條", @"社會", @"財經(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做標(biāo)記,方便后面索引,為了不出沖突,就把這個數(shù)值設(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 - 標(biāo)題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]; // 帶動畫切換到對應(yīng)的內(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)整標(biā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; // 因為設(shè)置了到邊不能滑動,所以不考慮邊界 UIButton *leftTitleButton = [_titleScrollView viewWithTag:1000 + leftTitleIndex]; UIButton *rightTitleButton = [_titleScrollView viewWithTag:1000 + rightTitleIndex]; // 設(shè)置大小和顏色漸變以及小橫線的聯(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)整標(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)題 // 這個偏移量是相對于scrollview的content frame原點的相對對標(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]; }
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.cdcxhl.com,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。