這篇文章主要為大家展示了“iOS如何實現(xiàn)仿網(wǎng)易新聞滾動導航條效果”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“iOS如何實現(xiàn)仿網(wǎng)易新聞滾動導航條效果”這篇文章吧。
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設,商丘企業(yè)網(wǎng)站建設,商丘品牌網(wǎng)站建設,網(wǎng)站定制,商丘網(wǎng)站建設報價,網(wǎng)絡營銷,網(wǎng)絡優(yōu)化,商丘網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。具體內容如下
實現(xiàn)效果
效果:選擇不同的欄目,下面出現(xiàn)不同的視圖,欄目條可以滾動;下面的視圖也可以滾動,滾動時上面對應的欄目要選中顏色為紅色;
滾動的導航條包括兩部分:標題滾動視圖(UIScrollView),內容滾動視圖(UIScrollView)
實現(xiàn)代碼
1.首先實現(xiàn)Main.storyboard
2.創(chuàng)建多個子控制器:頭條、科技、汽車、體育、視頻、圖片、熱點
// 頭條ViewController, 其它控制器和這個控制器都一樣,只是背景顏色不同而已 #import@interface TopLineViewController : UIViewController @end //---------------------------------------------------------------- #import "TopLineViewController.h" @interface TopLineViewController () @end @implementation TopLineViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; } @end
實現(xiàn)Main.storyboard對應的視圖控制器ViewController
#import@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 () @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. 初始化標題滾動視圖上的按鈕 [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; // 禁止額外滾動區(qū)域 self.automaticallyAdjustsScrollViewInsets = NO; // 初始化時默認選中第一個 [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; // 選中按鈕時要讓選中的按鈕居中 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 { } // 滾動結束時,將對應的視圖控制器的視圖添加到內容滾動視圖中 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { NSInteger i = self.contentScrollView.contentOffset.x / ScreenWidth; [self addViewToContentScrollView:_buttons[i]]; // 內容滾動視圖結束后選中對應的標題按鈕 [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.汽車 CarViewController * carViewController = [[CarViewController alloc] init]; carViewController.title = @"汽車"; [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.熱點 HotViewController * hotViewController = [[HotViewController alloc] init]; hotViewController.title = @"熱點"; [self addChildViewController:hotViewController]; } - (NSMutableArray *)buttons { if (_buttons == nil) { _buttons = [NSMutableArray array]; } return _buttons; } @end
以上是“iOS如何實現(xiàn)仿網(wǎng)易新聞滾動導航條效果”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注創(chuàng)新互聯(lián)網(wǎng)站建設公司行業(yè)資訊頻道!
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)建站www.cdcxhl.com,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。