免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1598 | 回复: 0
打印 上一主题 下一主题

[iOS] iOS开发之多表视图滑动切换示例(仿"头条"客户端) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-01 09:52 |只看该作者 |倒序浏览
来自:cnblog
好长时间没为大家带来iOS开发干货的东西了,今天给大家分享一个头条新闻客户端各个类别进行切换的一个示例。在Demo中对所需的组件进行的简单封装,在封装的组件中使用的是纯代码的形式,如果想要在项目中进行使用,稍微进行修改即可。

  废话少说,先介绍一下功能点,下图是整个Demo的功能点,最上面左边的TabBarButtonItem是用来减少条目的,比如下图有三个按钮,点击减号会减少一个条目。右边的为增加一个条目。点击相应的按钮是切换到对应的表视图上,下方红色的是滑动的指示器,同时支持手势滑动。运行具体效果如下图所示。

一:实现方案
最上方是一个View, View上面实例化了一些按钮,平分屏幕的宽度,下方是一个ScrollView, ScrollView上面放了一些表视图,点击不同的Button, 滑动到对应的表示图上。除了点击按钮,还可以进行滑动切换,切换时,红色的指示器也会随之滑动。
主要的技术点就是通过ScrollView的回调,通过事件的响应来改变ScrollView的ContentOffset的值。在回调中根据ContentOffset的值来计算红色指示器的偏移量。

二:核心代码
1.组件中的主要属性
把上面整个视图进行了封装,命名为SlideTabBarView,下面的代码是主要属性:
  1. @interface SlideTabBarView()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>
  2. 2 ///@brife 整个视图的大小
  3. 3 @property (assign) CGRect mViewFrame;
  4. 4
  5. 5 ///@brife 下方的ScrollView
  6. 6 @property (strong, nonatomic) UIScrollView *scrollView;
  7. 7
  8. 8 ///@brife 上方的按钮数组
  9. 9 @property (strong, nonatomic) NSMutableArray *topViews;
  10. 10
  11. 11 ///@brife 下方的表格数组
  12. 12 @property (strong, nonatomic) NSMutableArray *scrollTableViews;
  13. 13
  14. 14 ///@brife TableViews的数据源
  15. 15 @property (strong, nonatomic) NSMutableArray *dataSource;
  16. 16
  17. 17 ///@brife 当前选中页数
  18. 18 @property (assign) NSInteger currentPage;
  19. 19
  20. 20 ///@brife 下面滑动的View
  21. 21 @property (strong, nonatomic) UIView *slideView;
  22. 22 @end
复制代码
2.初始化方法如下,在调用初始化方法时需要传入SlideTabBarView的frame和选项卡的个数,初始化函数会调用一系列的初始化方法对组件进行初始化,代码如下:
  1. -(instancetype)initWithFrame:(CGRect)frame WithCount: (NSInteger) count{
  2. 2     self = [super initWithFrame:frame];
  3. 3     
  4. 4     if (self) {
  5. 5         _mViewFrame = frame;
  6. 6         _tabCount = count;
  7. 7         _topViews = [[NSMutableArray alloc] init];
  8. 8         _scrollTableViews = [[NSMutableArray alloc] init];
  9. 9         
  10. 10         [self initDataSource];
  11. 11         
  12. 12         [self initScrollView];
  13. 13         
  14. 14         [self initTopTabs];
  15. 15         
  16. 16         [self initDownTables];
  17. 17         
  18. 18         [self initDataSource];
  19. 19         
  20. 20         [self initSlideView];
  21. 21         
  22. 22     }
  23. 23     
  24. 24     return self;
  25. 25 }
复制代码
3.initDataSource方法主要负责模拟生成下方TableView要显示的数据。代码如下:
  1. #pragma mark -- 初始化表格的数据源
  2. -(void) initDataSource{
  3.     _dataSource = [[NSMutableArray alloc] initWithCapacity:_tabCount];
  4.    
  5.     for (int i = 1; i <= _tabCount; i ++) {
  6.         
  7.         NSMutableArray *tempArray  = [[NSMutableArray alloc] initWithCapacity:20];
  8.         
  9.         for (int j = 1; j <= 20; j ++) {
  10.             
  11.             NSString *tempStr = [NSString stringWithFormat:@"我是第%d个TableView的第%d条数据。", i, j];
  12.             [tempArray addObject:tempStr];
  13.         }
  14.         
  15.         [_dataSource addObject:tempArray];
  16.     }
  17. }
复制代码
4.红色滑动指示器的初始化代码如下所示:
  1. #pragma mark -- 初始化滑动的指示View
  2. -(void) initSlideView{
  3.      CGFloat width = _mViewFrame.size.width / _tabCount;
  4.     _slideView = [[UIView alloc] initWithFrame:CGRectMake(0, TOPHEIGHT - 5, width, 5)];
  5.     [_slideView setBackgroundColor:[UIColor redColor]];
  6.     [self addSubview:_slideView];
  7. }
复制代码
5.ScrollView的初始化代码如下, 指定ScrollView的大小位置以及背景颜色,并且设置分页可用并添加代理。
  1. #pragma mark -- 实例化ScrollView
  2. -(void) initScrollView{
  3.     _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, _mViewFrame.origin.y, _mViewFrame.size.width, _mViewFrame.size.height - TOPHEIGHT)];
  4.     _scrollView.contentSize = CGSizeMake(_mViewFrame.size.width * _tabCount, _mViewFrame.size.height - 60);
  5.     _scrollView.backgroundColor = [UIColor grayColor];
  6.    
  7.     _scrollView.pagingEnabled = YES;
  8.    
  9.     _scrollView.delegate = self;
  10.     [self addSubview:_scrollView];
  11. }
复制代码
6.添加上方的按钮,根据传入的个数来实例化多个按钮。
  1. #pragma mark -- 实例化顶部的tab
  2. 2 -(void) initTopTabs{
  3. 3     CGFloat width = _mViewFrame.size.width / _tabCount;
  4. 4     
  5. 5     for (int i = 0; i < _tabCount; i ++) {
  6. 6         
  7. 7         UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i * width, 0, width, TOPHEIGHT)];
  8. 8         
  9. 9         view.backgroundColor = [UIColor lightGrayColor];
  10. 10         
  11. 11         if (i % 2) {
  12. 12             view.backgroundColor = [UIColor grayColor];
  13. 13         }
  14. 14         
  15. 15         UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, width, TOPHEIGHT)];
  16. 16         button.tag = i;
  17. 17         [button setTitle:[NSString stringWithFormat:@"按钮%d", i+1] forState:UIControlStateNormal];
  18. 18         [button addTarget:self action:@selector(tabButton:) forControlEvents:UIControlEventTouchUpInside];
  19. 19         [view addSubview:button];
  20. 20         
  21. 21         
  22. 22         [_topViews addObject:view];
  23. 23         [self addSubview:view];
  24. 24     }
  25. 25 }
复制代码
7.点击按钮触发的方法如下:
  1. #pragma mark --点击顶部的按钮所触发的方法
  2. 2 -(void) tabButton: (id) sender{
  3. 3     UIButton *button = sender;
  4. 4     [_scrollView setContentOffset:CGPointMake(button.tag * _mViewFrame.size.width, 0) animated:YES];
  5. 5 }
复制代码
8.初始化下方的多个表视图:实例化表视图,并指定委托回调。
  1. #pragma mark --初始化下方的TableViews
  2. 2 -(void) initDownTables{
  3. 3     
  4. 4     for (int i = 0; i < _tabCount; i ++) {
  5. 5         
  6. 6         UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(i * _mViewFrame.size.width, 0, _mViewFrame.size.width, _mViewFrame.size.height - TOPHEIGHT)];
  7. 7         tableView.delegate = self;
  8. 8         tableView.dataSource = self;
  9. 9         
  10. 10         [_scrollTableViews addObject:tableView];
  11. 11         [_scrollView addSubview:tableView];
  12. 12     }
  13. 13
  14. 14 }
复制代码
9.ScrollView的回调方法如下,下面最后一个代理方法是根据ScrollView的偏移量来计算红色指示器的偏移量,第二个是滑动到哪个tableView,然后进行哪个TableView的数据加载。
  1. #pragma mark -- scrollView的代理方法
  2. 2 -(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
  3. 3     [self scrollViewDidEndDecelerating:scrollView];
  4. 4 }
  5. 5
  6. 6 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  7. 7
  8. 8 {
  9. 9     _currentPage = _scrollView.contentOffset.x/_mViewFrame.size.width;
  10. 10     
  11. 11     UITableView *currentTable = _scrollTableViews[_currentPage];
  12. 12     [currentTable reloadData];
  13. 13     
  14. 14 }
  15. 15
  16. 16 -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
  17. 17     if ([_scrollView isEqual:scrollView]) {
  18. 18         CGRect frame = _slideView.frame;
  19. 19         frame.origin.x = scrollView.contentOffset.x/_tabCount;
  20. 20         _slideView.frame = frame;
  21. 21     }
  22. 22 }
复制代码
10.TableView的代理方法如下,数据源就是我们刚才做的假数据,Cell是由Xib实现的,使用的时候注册一下就可用了。
  1. #pragma mark -- talbeView的代理方法
  2. 2 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  3. 3     return 1;
  4. 4 }
  5. 5
  6. 6 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  7. 7     NSMutableArray *tempArray = _dataSource[_currentPage];
  8. 8     return tempArray.count;
  9. 9 }
  10. 10
  11. 11 -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  12. 12     return 60;
  13. 13 }
  14. 14
  15. 15 -(UITableViewCell *)tableView:tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  16. 16     
  17. 17     BOOL nibsRegistered=NO;
  18. 18     if (!nibsRegistered) {
  19. 19         UINib *nib=[UINib nibWithNibName:@"SlideBarCell" bundle:nil];
  20. 20         [tableView registerNib:nib forCellReuseIdentifier:@"SlideBarCell"];
  21. 21         nibsRegistered=YES;
  22. 22     }
  23. 23     
  24. 24     
  25. 25     SlideBarCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SlideBarCell"];
  26. 26     if ([tableView isEqual:_scrollTableViews[_currentPage]]) {
  27. 27          cell.tipTitle.text = _dataSource[_currentPage][indexPath.row];
  28. 28     }
  29. 29   
  30. 30     return cell;
  31. 31 }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP