麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統 > iOS > 正文

iOS仿網易新聞滾動導航條效果

2019-10-21 18:41:05
字體:
來源:轉載
供稿:網友

本文實例為大家分享了iOS滾動導航條效果展示的具體代碼,供大家參考,具體內容如下

實現效果

效果:選擇不同的欄目,下面出現不同的視圖,欄目條可以滾動;下面的視圖也可以滾動,滾動時上面對應的欄目要選中顏色為紅色;

滾動的導航條包括兩部分:標題滾動視圖(UIScrollView),內容滾動視圖(UIScrollView)

iOS,滾動,導航條

實現代碼

1.首先實現Main.storyboard

iOS,滾動,導航條

2.創建多個子控制器:頭條、科技、汽車、體育、視頻、圖片、熱點

// 頭條ViewController, 其它控制器和這個控制器都一樣,只是背景顏色不同而已#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

實現Main.storyboard對應的視圖控制器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 = @"網易新聞";  // 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;  // 禁止額外滾動區域  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

以上代碼即可實現網易新聞 滾動的導航條, 因該功能可能在其它地方使用,所以最好可以將該功能抽出來,便于其它控制器集成,暫時還沒做。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 亚洲九九爱| 999久久久国产999久久久 | 欧美日韩视频网站 | 精品国产一区二区三区久久久狼牙 | 国产91在线播放九色 | 黄色av免费 | 性插视频 | 久久久三区 | 青青草最新网址 | 吾色视频 | 欧美a久久| av之家在线观看 | 欧美1区2区 | 羞羞色院91精品网站 | 欧美精品成人一区二区在线观看 | 久草在线手机视频 | 双性帝王调教跪撅打屁股 | 久久久久性 | 国产xxxxx在线观看 | 国产精品999在线观看 | 在线播放黄色片 | 国产一区二区免费 | 羞羞的视频在线免费观看 | 午夜神马电影网 | 欧美h版在线观看 | 亚洲精品一区二区三区免 | 精品久久久久久久久久久久久 | 国产亚洲综合一区二区 | 男女无遮挡羞羞视频 | v天堂在线视频 | 99在线精品视频免费观看20 | 国产免费视频一区二区裸体 | 国产精品99久久久久久大便 | 99sesese| 一区二区三区在线视频观看58 | 黄色成人在线 | 日日草夜夜 | 草草影院地址 | 日韩激情一区 | 国产精品久久久久一区二区 | 亚洲精品久久久久久下一站 |