iOS滚动的导航条(仿网易新闻)
实现效果
效果:选择不同的栏目,下面出现不同的视图,栏目条可以滚动;下面的视图也可以滚动,滚动时上面对应的栏目要选中颜色为红色;
滚动的导航条包括两部分:标题滚动视图(UIScrollView),内容滚动视图(UIScrollView)
实现代码
首先实现Main.storyboard
创建多个子控制器:头条、科技、汽车、体育、视频、图片、热点
// 头条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
以上代码即可实现网易新闻 滚动的导航条, 因该功能可能在其它地方使用,所以最好可以将该功能抽出来,便于其它控制器集成,暂时还没做
iOS滚动的导航条(仿网易新闻)相关推荐
- 仿网易新闻顶部菜单html,iOS仿网易新闻滚动导航条效果
本文实例为大家分享了iOS滚动导航条效果展示的具体代码,供大家参考,具体内容如下 实现效果 效果:选择不同的栏目,下面出现不同的视图,栏目条可以滚动:下面的视图也可以滚动,滚动时上面对应的栏目要选中颜 ...
- IOS开发——仿网易新闻客户端
IOS开发--仿网易新闻客户端 本文没有内容,传个资源 衔接地址:http://download.csdn.net/detail/u012881779/7152281 左侧导航部分: 新闻版块 订阅版 ...
- 仿网易新闻客户端的上面的tab和下面的功能条
2019独角兽企业重金招聘Python工程师标准>>> 仿网易新闻客户端的上面的tab和下面的功能条 package com.and.netease; import com.and. ...
- Android 开源框架ViewPageIndicator 和 ViewPager 仿网易新闻客户端Tab标签
转载请注明出处:http://blog.csdn.net/xiaanming/article/details/10766053 之前用JakeWharton的开源框架ActionBarSherl ...
- 网易新闻 html5,HTML5+SWIPER仿网易新闻横滑翻页及联动
[实例简介] 一套仿网易新闻的闻横滑翻页,联动导航,用到了swiper.js zepto.js scroll.js效果不错 [实例截图] [核心代码] swiper └── swiper ├── cs ...
- 仿网易新闻的页面(ViewPager作为RecyclerView的Header)
需求 > 想实现一个仿网易新闻的页面,上面是轮播的图片,下面是 RecyclerView 显示新闻列表. 本文链接 http://blog.csdn.net/never_cxb/article/ ...
- android分类功能,Android 仿网易新闻客户端分类排序功能
先来看看网易新闻客户端以及自己实现的效果图,效果当然还是网易的好 gridviewsort.gif 如何实现拖拽一个Item 用WindowManager添加一个ImageView,并且将这个Imag ...
- Android高仿网易新闻客户端之动态添加标签
承接上一篇文章:Android高仿网易新闻客户端之首页,今天来实现动态添加标签效果. 动态标签页是一个流式布局,实现了宽度自动换行高度自动分配的功能,代码如下: FlowLayout.java pac ...
- 仿网易新闻客户端UI界面小Demo
图一 图二 图三 仿网易新闻客户端UI界面Demo 图一:新闻模块 UI界面:点击下方按钮,显示相应内容的页面信息 图三:点击新闻模块上方按钮显示体育,娱乐,科技等页面,相应的内容. 所涉及的知识点: ...
最新文章
- Qt中文手册 之 QTreeWidgetItem
- 从对ML一窍不通到斩获AT等special offer,拿下大厂算法岗就靠它了
- php barcode_php生成条形码
- 测试电阻电容 二三极管的好帮手 晶体管测试显示模块
- FPGA基础知识极简教程(5)什么是锁存器以及如何在FPGA开发中避免生成锁存器?
- ffmpeg php win32,解决PHP5.3.x下ffmpeg安装配置问题
- vue获取商品数据接口_基于 request cache 请求缓存技术优化批量商品数据查询接口...
- 如何保持纯洁男女关系……
- 利用百度进行URL编码解码
- C语言创建学生姓名分数链表,C语言编程 编写程序,建立一个学生数据链表,学生的数据包括学号、姓名、成绩。...
- Unity读取TXT文本文件
- OpenV2X 标准整理
- 联想电脑linux显卡驱动,哪里下载独立显卡驱动 急急急!!联想y470如何在linux下安装显卡驱动啊?你好...
- 中台方法论及案例集合
- 什么是学习能力?如何提高学习能力?
- 2022化工自动化控制仪表考试试题及模拟考试
- 瑞幸退市,董事长被罢免,但是我并不同情他!
- 下载 idm 迅雷 网盘 磁力
- gmai邮箱怎么注册啊
- 跟着柴毛毛学Spring(1)——纵观Spring
热门文章
- Simulink三相异步电机仿真(1)
- jude(java建模软件)_JUDE(JAVA建模软件)下载
- 遇见Laravel Migrations的migrate与rollback
- 技术实践|Redis基础知识及集群搭建(上)
- Word文档中多个编号放同一行的方法(非技术)
- 基于改进二进制粒子群算法的配电网重构(matlab实现)
- zeros什么意思_matlab中zeros函数是什么含义?MATLAB中zeros表示表示什么意思
- riscv-amo原子指令
- MySQL + Oracle GoldenGate + OGG Application Adpater
- 立足小餐饮,“新名酒”江小白能走多远?