iOS仿網(wǎng)易新聞滾動(dòng)導(dǎo)航條效果
本文實(shí)例為大家分享了iOS滾動(dòng)導(dǎo)航條效果展示的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)效果
效果:選擇不同的欄目,下面出現(xiàn)不同的視圖,欄目條可以滾動(dòng);下面的視圖也可以滾動(dòng),滾動(dòng)時(shí)上面對(duì)應(yīng)的欄目要選中顏色為紅色;
滾動(dòng)的導(dǎo)航條包括兩部分:標(biāo)題滾動(dòng)視圖(UIScrollView),內(nèi)容滾動(dòng)視圖(UIScrollView)
實(shí)現(xiàn)代碼
1.首先實(shí)現(xiàn)Main.storyboard
2.創(chuàng)建多個(gè)子控制器:頭條、科技、汽車(chē)、體育、視頻、圖片、熱點(diǎn)
// 頭條ViewController, 其它控制器和這個(gè)控制器都一樣,只是背景顏色不同而已 #import <UIKit/UIKit.h> @interface TopLineViewController : UIViewController @end //---------------------------------------------------------------- #import "TopLineViewController.h" @interface TopLineViewController () @end @implementation TopLineViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; } @end
實(shí)現(xiàn)Main.storyboard對(duì)應(yīng)的視圖控制器ViewController
#import <UIKit/UIKit.h> @interface ViewController : UIViewController @end //---------------------------------------------------------------- #import "ViewController.h" #import "TopLineViewController.h" #import "TechnologyViewController.h" #import "CarViewController.h" #import "SportsViewController.h" #import "VideoViewController.h" #import "ImageViewController.h" #import "HotViewController.h" #define ScreenWidth [UIScreen mainScreen].bounds.size.width #define ScreenHeight [UIScreen mainScreen].bounds.size.height @interface ViewController () <UIScrollViewDelegate> @property (weak, nonatomic) IBOutlet UIScrollView *titleScrollView; @property (weak, nonatomic) IBOutlet UIScrollView *contentScrollView; @property (strong, nonatomic) NSMutableArray *buttons; @property (strong, nonatomic) UIButton *selectedButton; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title = @"網(wǎng)易新聞"; // 1. 初始化標(biāo)題滾動(dòng)視圖上的按鈕 [self initButtonsForButtonScrollView]; } - (void) initButtonsForButtonScrollView { // 初始化子控制器 [self initChildViewControllers]; CGFloat buttonWidth = 100; CGFloat buttonHeight = 40; NSInteger childViewControllerCount = self.childViewControllers.count; for (NSInteger i = 0; i < childViewControllerCount; i++) { UIViewController *childViewController = self.childViewControllers[i]; UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom]; titleButton.tag = i; CGFloat x = i * buttonWidth; titleButton.frame = CGRectMake(x, 0, buttonWidth, buttonHeight); [titleButton setTitle:childViewController.title forState:UIControlStateNormal]; [titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [titleButton addTarget:self action:@selector(titleButtonOnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.titleScrollView addSubview:titleButton]; [self.buttons addObject:titleButton]; } self.titleScrollView.contentSize = CGSizeMake(buttonWidth * childViewControllerCount, 0); self.titleScrollView.showsHorizontalScrollIndicator = NO; self.titleScrollView.bounces = NO; self.contentScrollView.contentSize = CGSizeMake(ScreenWidth * childViewControllerCount, 0); self.contentScrollView.showsHorizontalScrollIndicator = NO; self.contentScrollView.pagingEnabled = YES; self.contentScrollView.delegate = self; // 禁止額外滾動(dòng)區(qū)域 self.automaticallyAdjustsScrollViewInsets = NO; // 初始化時(shí)默認(rèn)選中第一個(gè) [self titleButtonOnClick:self.buttons[0]]; } - (void)titleButtonOnClick:(UIButton *)button { // 1. 選中按鈕 [self selectingButton:button]; // 2. 顯示子視圖 [self addViewToContentScrollView:button]; } - (void)selectingButton:(UIButton *)button { [_selectedButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; _selectedButton.transform = CGAffineTransformMakeScale(1.0, 1.0); [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; button.transform = CGAffineTransformMakeScale(1.3, 1.3); // 選中字體變大,按鈕變大,字體也跟著變大 _selectedButton = button; // 選中按鈕時(shí)要讓選中的按鈕居中 CGFloat offsetX = button.frame.origin.x - ScreenWidth * 0.5; CGFloat maxOffsetX = self.titleScrollView.contentSize.width - ScreenWidth; if (offsetX < 0) { offsetX = 0; } else if (offsetX > maxOffsetX) { offsetX = maxOffsetX; } [self.titleScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES]; } - (void)addViewToContentScrollView:(UIButton *)button { NSInteger i = button.tag; UIViewController *childViewController = self.childViewControllers[i]; CGFloat x = i * ScreenWidth; // 防止添加多次 if (childViewController.view.subviews != nil) { childViewController.view.frame = CGRectMake(x, 0, ScreenWidth, ScreenHeight); [self.contentScrollView addSubview:childViewController.view]; } self.contentScrollView.contentOffset = CGPointMake(x, 0); } #pragma mark - UIScrollViewDelegate - (void)scrollViewDidScroll:(UIScrollView *)scrollView { } // 滾動(dòng)結(jié)束時(shí),將對(duì)應(yīng)的視圖控制器的視圖添加到內(nèi)容滾動(dòng)視圖中 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { NSInteger i = self.contentScrollView.contentOffset.x / ScreenWidth; [self addViewToContentScrollView:_buttons[i]]; // 內(nèi)容滾動(dòng)視圖結(jié)束后選中對(duì)應(yīng)的標(biāo)題按鈕 [self selectingButton:_buttons[i]]; } - (void)initChildViewControllers { // 0.頭條 TopLineViewController * topViewController = [[TopLineViewController alloc] init]; topViewController.title = @"頭條"; [self addChildViewController:topViewController]; // 1.科技 TechnologyViewController * technologyViewController = [[TechnologyViewController alloc] init]; technologyViewController.title = @"科技"; [self addChildViewController:technologyViewController]; // 2.汽車(chē) CarViewController * carViewController = [[CarViewController alloc] init]; carViewController.title = @"汽車(chē)"; [self addChildViewController:carViewController]; // 3.體育 SportsViewController * sportsViewController = [[SportsViewController alloc] init]; sportsViewController.title = @"體育"; [self addChildViewController:sportsViewController]; // 4.視頻 VideoViewController * videoViewController = [[VideoViewController alloc] init]; videoViewController.title = @"視頻"; [self addChildViewController:videoViewController]; // 5.圖片 ImageViewController * imageViewController = [[ImageViewController alloc] init]; imageViewController.title = @"圖片"; [self addChildViewController:imageViewController]; // 6.熱點(diǎn) HotViewController * hotViewController = [[HotViewController alloc] init]; hotViewController.title = @"熱點(diǎn)"; [self addChildViewController:hotViewController]; } - (NSMutableArray *)buttons { if (_buttons == nil) { _buttons = [NSMutableArray array]; } return _buttons; } @end
以上代碼即可實(shí)現(xiàn)網(wǎng)易新聞 滾動(dòng)的導(dǎo)航條, 因該功能可能在其它地方使用,所以最好可以將該功能抽出來(lái),便于其它控制器集成,暫時(shí)還沒(méi)做。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- iOS實(shí)現(xiàn)卡片式滾動(dòng)效果 iOS實(shí)現(xiàn)電影選片效果
- iOS ScrollView嵌套tableView聯(lián)動(dòng)滾動(dòng)的思路與最佳實(shí)踐
- iOS使用UICollectionView實(shí)現(xiàn)橫向滾動(dòng)照片效果
- iOS自定義時(shí)間滾動(dòng)選擇控件
- iOS自定義水平滾動(dòng)條、進(jìn)度條
- iOS自定義可展示、交互的scrollView滾動(dòng)條
- iOS Swift UICollectionView橫向分頁(yè)滾動(dòng),cell左右排版問(wèn)題詳解
- iOS?實(shí)現(xiàn)類(lèi)似抖音滾動(dòng)效果
相關(guān)文章
ios的手勢(shì)操作之UIGestureRecognizer淺析(推薦)
本篇文章主要介紹了ios的手勢(shì)操作之UIGestureRecognizer淺析,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。2016-12-12iOS實(shí)現(xiàn)圖片水印與簡(jiǎn)單封裝示例代碼
這篇文章主要給大家介紹了關(guān)于iOS實(shí)現(xiàn)圖片水印與簡(jiǎn)單封裝的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01基于ios逆向過(guò)程中l(wèi)ldb調(diào)試技巧(推薦)
下面小編就為大家?guī)?lái)一篇基于ios逆向過(guò)程中l(wèi)ldb調(diào)試技巧(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07Flutter?模型動(dòng)態(tài)化賦值研究分析
這篇文章主要為大家介紹了Flutter?模型動(dòng)態(tài)化賦值研究分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03iOS動(dòng)態(tài)調(diào)整UILabel高度的幾種方法
在iOS編程中UILabel是一個(gè)常用的控件,下面這篇文章主要給大家介紹了關(guān)于iOS動(dòng)態(tài)調(diào)整UILabel高度的幾種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12iOS實(shí)現(xiàn)左右可滑動(dòng)的選擇條實(shí)例代碼分享
本文通過(guò)實(shí)例代碼給大家介紹了ios實(shí)現(xiàn)左右可滑動(dòng)的選擇條功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-03-03利用iOS手勢(shì)與scrollView代理實(shí)現(xiàn)圖片的放大縮小
這篇文章主要介紹了利用iOS的手勢(shì)、scrollView代理來(lái)實(shí)現(xiàn)圖片放大縮小的方法,文中通過(guò)示例代碼介紹的很詳細(xì),相信對(duì)各位iOS開(kāi)發(fā)者們來(lái)說(shuō)具有一定的參考借鑒價(jià)值,有需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-01-01