功能介绍

最近在做一款ios的app,其中有一个页面需要分成三个版块,版块之间可以通过左右滑动来进行切换,也可以通过点击上方的按钮来切换,好像在android中可以用过ViewPager + Fragment来实现。界面大概就是这个样子,这里我以知乎客户端的发现页面为例:

 
 

其中整个页面分为三个小的版块,分别"推荐","热门"以及"收藏"。点击上方的文字后,下方的页面会切换到对应的版块,同时文字下方的蓝色导航条也会随着界面的切换跟着滑动。另外,当我们不通过按钮切换,用手势在页面上左右滑动时,导航条也会跟着一起滑动。功能大概就是这个样子。

原理

下面简单分析下实现原理。首先在"发现"的下方应该有UIView,在UIView中放有3个UIButton。至于文字下方的导航条,可以用UILabel来做,宽度等于一个按钮的宽度,并将背景色改为蓝色。导航条下方的页面可以通过手势来左右滑动,首先想到的应该就是,这是一个UIScrollView,我们需要将scrollView的contentSize属性设置成三个页面宽度的大小,否则会无法滑动页面。然后在scrollView放上三个版块的页面,这个根据自身的业务情况而定,一般UITableView用得比较多。最后,怎样才能让页面滑动的时候导航条也跟着移动的?在Android中,一般的做法就是给scrollView中添加一个事件监听器,用来监听滑动事件,并在事件处理函数中执行响应的逻辑。在ios中没有事件监听这个概念,我们可以为scrollView设置它的代理,并在代理类中覆盖掉该方法:

  1. - (void)scrollViewDidScroll:(UIScrollView *)scrollView

设置了代理类后,代理类其实就是帮我们注册了相应的回调函数,每次scrollView只要有细微的滑动,该方法就会被调用。在方法中,需要做的就是获取scrollView当前的偏移量,然后根据这个偏移量来设定导航条的位置。

代码实现

将scrollView与滚动条声明未全局变量,因为在其他方法中也会用到。

  1. //存放三个按钮的父页面宽度
  2. #define TOP_VIEW_HEIGHT 50
  3. //界面上方导航线条的宽度
  4. #define NAVIGATION_LINE_HEIGHT 2
  5. @interface MyMesgViewController () <UIScrollViewDelegate>
  6. @property (nonatomic, strong) UIScrollView *scrollView;
  7. @property (nonatomic, strong) UILabel *navLabel;
  8. @end

scrollView延迟初始化,在初始化时记得为其设置代理对象。

  1. - (UIScrollView *)scrollView {
  2. if (!_scrollView) {
  3. CGFloat scrollViewHeight = 64 + TOP_VIEW_HEIGHT + 8;
  4. _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, scrollViewHeight, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - scrollViewHeight)];
  5. _scrollView.showsHorizontalScrollIndicator = NO;
  6. _scrollView.pagingEnabled = YES;
  7. CGSize size = _scrollView.frame.size;
  8. size.width *= 3;
  9. _scrollView.contentSize = size;
  10. _scrollView.backgroundColor = [UIColor whiteColor];
  11. _scrollView.delegate = self;
  12. [self.view addSubview:_scrollView];
  13. }
  14. return _scrollView;
  15. }

navLabel延迟初始化

  1. - (UILabel *)navLabel {
  2. if (!_navLabel) {
  3. _navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, TOP_VIEW_HEIGHT - NAVIGATION_LINE_HEIGHT, [UIScreen mainScreen].bounds.size.width / 3, NAVIGATION_LINE_HEIGHT)];
  4. _navLabel.backgroundColor = [UIColor blueColor];
  5. }
  6. return _navLabel;
  7. }

将三个按钮添加到页面中,并为按钮添加点击事件处理函数。

  1. - (void)addMessageCategoryButton {
  2. UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, TOP_VIEW_HEIGHT)];
  3. topView.backgroundColor = [UIColor whiteColor];
  4. UIButton *approvedMsgButton = [self createMsgButton:@"审核消息" posX:0];
  5. UIButton *mosMsgButton = [self createMsgButton:@"M币交易" posX:approvedMsgButton.frame.size.width];
  6. UIButton *systemMsgButton = [self createMsgButton:@"系统通知" posX:approvedMsgButton.frame.size.width * 2];
  7. [topView addSubview:approvedMsgButton];
  8. [topView addSubview:mosMsgButton];
  9. [topView addSubview:systemMsgButton];
  10. [topView addSubview:self.navLabel];
  11. [self.view addSubview:topView];
  12. }

将创建按钮的过程单独抽象出来

  1. - (UIButton *)createMsgButton:(NSString *)title posX:(CGFloat)posX {
  2. UIButton *msgButton = [[UIButton alloc] initWithFrame:CGRectMake(posX, 0, self.view.frame.size.width / 3, TOP_VIEW_HEIGHT - NAVIGATION_LINE_HEIGHT)];
  3. msgButton.backgroundColor = [UIColor whiteColor];
  4. NSAttributedString *attributeTitle = [[NSAttributedString alloc] initWithString:title attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:14] }];
  5. [msgButton setAttributedTitle:attributeTitle forState:UIControlStateNormal];
  6. msgButton.tag = (NSInteger) (posX / (self.view.frame.size.width / 3));
  7. [msgButton addTarget:self action:@selector(switchMessageDetailView:) forControlEvents:UIControlEventTouchUpInside];
  8. return msgButton;
  9. }

按钮点击后调用的函数,即切换页面

  1. - (void)switchMessageDetailView:(UIButton *)btn {
  2. [self.scrollView setContentOffset:CGPointMake(btn.tag * self.view.frame.size.width, 0) animated:YES];
  3. }

最后,在回调函数中根据scrollView的偏移量调整导航条的位置。

  1. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  2. CGPoint offset = scrollView.contentOffset;
  3. CGFloat x = offset.x / 3;
  4. if (x > 0 && x < scrollView.frame.size.width) {
  5. CGRect frame = self.navLabel.frame;
  6. frame.origin.x = x;
  7. self.navLabel.frame = frame;
  8. }
  9. }

这是最终的效果图:

 

iOS下简单实现滑动导航条的更多相关文章

  1. 基于jQuery鼠标悬停上下滑动导航条

    基于jQuery鼠标悬停上下滑动导航条.这是一款蓝色好看的鼠标响应式网站导航菜单特效.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div id="menu2& ...

  2. 【iOS开发-22】navigationBar导航条和navigationItem设置:基本搞定导航条上的文字和按钮以及各种跳转

    http://blog.csdn.net/weisubao/article/details/39646739?utm_source=tuicool&utm_medium=referral (1 ...

  3. iOS仿今日头条滑动导航

    之前写了篇博客网易首页导航封装类.网易首页导航封装类优化,今天在前两个的基础上仿下今日头条. 1.网易首页导航封装类中主要解决了上面导航的ScrollView和下面的页面的ScrollView联动的问 ...

  4. 【Andord真】SlideMenu+ViewPagerIndictor双滑动边栏+滑动导航条

    采取SlideMenu达到的效果侧边栏: 间 setContentView是设置主背景的布局 setBehindContentView是设置左边菜单的布局 setSecondaryMenu是设置右边的 ...

  5. 【原】Github系列之三:开源iOS下 渐变颜色的进度条WGradientProgress

    概述 今天我们来实现一个iOS平台上的进度条(progress bar or progress view).这种进度条比APPLE自带的更加漂亮,更加有“B格”.它拥有渐变的颜色,而且这种颜色是动态移 ...

  6. Bootstrap学习笔记(5)--实现Bootstrap导航条可点击和鼠标悬停显示下拉菜单

    实现Bootstrap导航条可点击和鼠标悬停显示下拉菜单 微笑的鱼 2014-01-03 Bootstrap 5,281 次围观 11条评论 使用Bootstrap导航条组件时,如果你的导航条带有下拉 ...

  7. 【转】一个DIV+CSS代码布局的简单导航条

    原文地址:http://www.divcss5.com/shili/s731.shtml 简单的DIV CSS代码布局实现导航条 一个蓝色主题的导航条布局案例,本CSS小实例,采用DIV CSS实现. ...

  8. ch6 列表和导航条

    为列表添加定制的项目符号 可使用list-style-image属性:缺点是对项目符号图像的位置的控制能力不强. 常用的方法:使用list-style-type来关闭项目符号,将定制的项目符号作为背景 ...

  9. bootstrap 组件之"导航条"

    一个典型的导航条基本代码如下: <nav class="navbar navbar-default"> <div class="container&qu ...

随机推荐

  1. Show Desktop Pro FAQ

    Q. Will the desktop background image be restored after quit? A: Yes. Right now, "Hide icons&quo ...

  2. Hibernate的懒加载session丢失解决方法

    在web.xml加入spring提供的过滤器,延长session的生命周期 <!--Hibernate的懒加载session丢失解决方法 --> <filter> <fi ...

  3. python-打印进度条

    progress_bar.py #!/usr/bin/python3.6 #__*__uft8__*__ import sys import time def progress(percent,wid ...

  4. 使用vs的时候,遇到这个:当前不会命中断点 还没有为该文档加载任何符号

    一 http://stackoverflow.com/questions/2155930/fixing-the-breakpoint-will-not-currently-be-hit-no-symb ...

  5. 推荐一个JavaScript触发器插件,可通过指定频次、指定时间内触发指定的处理函数

    推荐一个JavaScript触发器插件js-trigger js-trigger是一个JavaScript触发器插件,可通过指定频次.指定时间内触发指定的处理函数 https://tanwei-cc. ...

  6. 各类人工智能&大数据相关比赛

    比赛技巧:https://zhuanlan.zhihu.com/p/28084438 文章来源: https://www.imooc.com/article/72863 随着近几年人工智能和大数据的快 ...

  7. Glibc-2.3.4编译

    $tar xf Glibc2.3.4.tar.bz2 $mkdir build_glibc $cd build_glibc ../glibc-2.3.4/configure --prefix=/too ...

  8. Git 设置 SOCKS 代理

    $ export all_proxy=socks5://127.0.0.1:1080

  9. Linux虚拟机忘记root密码的拯救办法

    于是百度之,还是有很多解决方案的.其原理就是,进入到single单用户模式下,去修改root密码: 1.重启系统,选择Linux引导,然后按住e,出现如下界面: 按下e 出现如下页面 选择kernel ...

  10. [Tomcat 部署问题] Undeployment Failure could not be redeployed ...

    Tomcat 部署,在部署可能会出现以下问题: Deployment failure on Tomcat 6.x. Could not copy all resources to E:\apache- ...